upload&&download

package am.demo; 
import java.io.File; 
import java.io.IOException; 
import java.util.Iterator; 
import java.util.List; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import org.apache.commons.fileupload.FileItem; 
import org.apache.commons.fileupload.disk.DiskFileItemFactory; 
import org.apache.commons.fileupload.servlet.ServletFileUpload; 
@SuppressWarnings("serial") 
public class Upload extends HttpServlet { 
    private String uploadPath = "d://temp"; // 上传文件的目录 
    @SuppressWarnings("unchecked") 
    public void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws IOException, ServletException { 
        try { 
            // Create a factory for disk-based file items 
            DiskFileItemFactory factory = new DiskFileItemFactory(); 
            // Create a new file upload handler 
            ServletFileUpload upload = new ServletFileUpload(factory); 
            List<FileItem> items = upload.parseRequest(request);// 得到所有的文件 
            Iterator<FileItem> i = items.iterator();
            while (i.hasNext()) { 
                FileItem fi = (FileItem) i.next();
                String fileName = fi.getName();
                if (fileName != null) { 
                    File fullFile = new File(fi.getName()); 
                    File savedFile = new File(uploadPath, fullFile.getName()); 
                    fi.write(savedFile);
                }
            }
            response.setContentType("text/html;charset=GBK");   
            response.getWriter().print(
            "<mce:script language=‘javascript‘><!-- 
alert(‘上传成功‘);window.location.href=‘index.jsp‘; 
// --></mce:script>"); 
        } catch (Exception e) { 
            // 可以跳转出错页面 
            e.printStackTrace();
        }
    }
    public void init() throws ServletException { 
        File uploadFile = new File(uploadPath); 
        if (!uploadFile.exists()) { 
            uploadFile.mkdirs();
        }
    }
}

package am.demo; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.OutputStream; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
@SuppressWarnings("serial") 
public class Download extends HttpServlet { 
    public void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws IOException, ServletException { 
        String aFileName = new String(request.getParameter("name").getBytes( 
                "iso8859-1"), "gbk"); 
        File fileLoad = new File("d:/temp", aFileName); 
        FileInputStream in = null; // 输入流 
        OutputStream out = response.getOutputStream();
        byte b[] = new byte[1024]; 
        try { 
             response.setContentType("application/x-msdownload;"); 
            response.setHeader("Content-disposition", "attachment; filename=" 
                    + new String(aFileName.getBytes("GBK"), "ISO-8859-1")); 
            // download the file. 
            in = new FileInputStream(fileLoad); 
            int n = 0; 
            while ((n = in.read(b)) != -1) { 
                out.write(b, 0, n); 
            }
        } catch (Throwable e) { 
            e.printStackTrace();
        } finally { 
            try { 
                in.close();
                out.close();
            } catch (Throwable e) { 
                e.printStackTrace();
            }
        }
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws IOException, ServletException { 
        doGet(request, response);
    }
}

package am.demo; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
@SuppressWarnings("serial") 
public class Delete extends HttpServlet { 
    public void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws FileNotFoundException, IOException { 
        String aFileName = new String(request.getParameter("name").getBytes( 
                "iso8859-1"), "gbk"); 
        File file = new File("d:/temp", aFileName); 
        response.setContentType("text/html;charset=GBK"); 
        if (!file.isDirectory()) { 
            file.delete();
            response.getWriter().print(
                "<mce:script language=‘javascript‘><!-- 
alert(‘删除成功‘);window.location.href=‘index.jsp‘; 
// --></mce:script>"); 
        } else { 
        }
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws IOException, ServletException { 
        doGet(request, response);
    }
}

时间: 2024-11-06 03:31:06

upload&&download的相关文章

Asp.net core 学习笔记 ( upload/download files 文件上传与下载 )

2017-09-25 refer : https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads https://www.codeproject.com/Articles/1203408/Upload-Download-Files-in-ASP-NET-Core 这里只说说小文件上传. 先看看前端 js 代码 <input id="inputFile" type="file" /&g

flask file &amp; stream (upload/download/method/stream)

demo http://www.bjhee.com/flask-ad5.html cd flask-ad5 python flask-ad5.py 1) http://0.0.0.0:5000/ hello,world ! 2) http://0.0.0.0:5000/upload upload file 3) http://0.0.0.0:5000/large.csv download large.csv file 4) http://0.0.0.0:5000//method yield re

file upload download

1. 文件上传与下载 1.1 文件上传 案例: 注册表单/保存商品等相关模块! --à 注册选择头像 / 商品图片 (数据库:存储图片路径 / 图片保存到服务器中指定的目录) 文件上传,要点: 前台: 1. 提交方式:post 2. 表单中有文件上传的表单项: <input type="file" /> 3. 指定表单类型: 默认类型:enctype="application/x-www-form-urlencoded" 文件上传类型:multipart

jQuery File Upload 单页面多实例的实现

jQuery File Upload 的 GitHub 地址:https://github.com/blueimp/jQuery-File-Upload 插件描述:jQuery File Upload 是一个 jQuery 图片上传组件,支持多文件上传.取消.删除,上传前缩略图预览.列表显示图片大小,支持上传进度条显示.插件基于开放的标准,如 HTML5 和 JavaScript ,不需要额外的浏览器插件(例如使用Adobe 的 Flash ),在旧版浏览器中使用 XMLHttpRequest

MICS:副本和纠删码混合存储系统

摘要 云存储系统的三个指标: 高可靠性,低存储开销,高读写性能. 这三个指标是没有办法同一时候满足的,许多时候须要进行tradeoff. 副本系统和纠删码是两种在存储系统中广泛使用的策略,它们在保证高可靠性的前提下,选择了不同极端的tradeoff. 副本存储开销大,但性能较好.纠删码存储开销低.但性能较差.本文提出了MICS系统.它将一个对象以两种形式存储,一种採用副本.一种採用分片纠删码,不仅如此.还设计了针对这种hyprid结构的精细的读写协议. 在服务使用者的角度.MICS通过PRAM一

ansible安装配置与简单使用

前言: AnsibleWorks成立于2012年,由自动化工具Cobbler及Func的开发者Michael DeHaan创建.其Ansible平台是一个开源的配置及计算机管理平台.可实现多节点的软件部署,执行特定任务并进行配置管理. Ansible 跟其他IT自动化技术的区别在于其关注点并非配置管理.应用部署或IT流程工作流,而是提供一个统一的界面来协调所有的IT自动化功能,因此 Ansible的系统更加易用,部署更快.受管理的节点无需安装额外的远程控制软件,由平台通过SSH(Secure S

一键安装ftp服务器

1 概述 工作中,需要搭建ftp服务器来实现资源的共享.本文将通过脚本实现自动化安装ftp服务器,并进行相关配置,同时创建ftp登录账户,以及创建两个文件夹,作为公共的文件夹,为所有用户提供共同的访问路径. 2 概念解释和注意事项 2.1 chroot_list说明 chroot_list_file=/etc/vsftpd/chroot_list 如果设置为 chroot_local_user=YES chroot_list_enable=YES(这行可以没有, 也可以有) chroot_lis

视频编码与封装方式详解

http://blog.csdn.net/attilax/article/details/5807047 1.    编码方式和封装格式    1 2.    视频编码标准两大系统    2 MPEG-1. 2 MPEG-2. 3 MPEG-3. 3 MPEG-4. 3 ITU-T. 4 WMV.. 4 3.    常用视频编码方式有Xvid( 4 4.    常见存储封装格式    6 5.    几种常见的转换格式设置    7 6.    无损视频编码    9 1.   编码方式和封装格

Go语言(golang)开源项目大全

转http://www.open-open.com/lib/view/open1396063913278.html内容目录Astronomy构建工具缓存云计算命令行选项解析器命令行工具压缩配置文件解析器控制台用户界面加密数据处理数据结构数据库和存储开发工具分布式/网格计算文档编辑器Encodings and Character SetsGamesGISGo ImplementationsGraphics and AudioGUIs and Widget ToolkitsHardwareLangu