1. 文件上传与下载

1.1 文件上传

案例:

注册表单/保存商品等相关模块!

--à 注册选择头像 / 商品图片

(数据库:存储图片路径 / 图片保存到服务器中指定的目录)

文件上传,要点:

前台:

1. 提交方式:post

2. 表单中有文件上传的表单项: <input type=”file” />

3. 指定表单类型:

默认类型:enctype="application/x-www-form-urlencoded"

文件上传类型:multipart/form-data

手动实现文件上传

<body>
<form name="frm_test" action="${pageContext.request.contextPath }/upload" method="post" enctype="multipart/form-data">
用户名:<input type="text" name="userName"> <br/>
文件: <input type="file" name="file_img"> <br/> <input type="submit" value="注册">
</form>
</body>
public class UploadServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/*
request.getParameter(""); // GET/POST
request.getQueryString(); // 获取GET提交的数据
request.getInputStream(); // 获取post提交的数据 */ /***********手动获取文件上传表单数据************/ //1. 获取表单数据流
InputStream in = request.getInputStream();
//2. 转换流
InputStreamReader inStream = new InputStreamReader(in, "UTF-8");
//3. 缓冲流
BufferedReader reader = new BufferedReader(inStream);
// 输出数据
String str = null;
while ((str = reader.readLine()) != null) {
System.out.println(str);
} // 关闭
reader.close();
inStream.close();
in.close();
}

Apache提供的文件上传组件:FileUpload组件

文件上传功能开发中比较常用,apache也提供了文件上传组件!

FileUpload组件:

1. 下载源码

2. 项目中引入jar文件

commons-fileupload-1.2.1.jar  【文件上传组件核心jar包】

commons-io-1.4.jar     【封装了对文件处理的相关工具类】

public class UploadServlet extends HttpServlet {

    // upload目录,保存上传的资源
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { /*********文件上传组件: 处理文件上传************/ try {
// 1. 文件上传工厂
FileItemFactory factory = new DiskFileItemFactory();
// 2. 创建文件上传核心工具类
ServletFileUpload upload = new ServletFileUpload(factory); // 一、设置单个文件允许的最大的大小: 30M
upload.setFileSizeMax(30*1024*1024);
// 二、设置文件上传表单允许的总大小: 80M
upload.setSizeMax(80*1024*1024);
// 三、 设置上传表单文件名的编码
// 相当于:request.setCharacterEncoding("UTF-8");
upload.setHeaderEncoding("UTF-8"); // 3. 判断: 当前表单是否为文件上传表单
if (upload.isMultipartContent(request)){
// 4. 把请求数据转换为一个个FileItem对象,再用集合封装
List<FileItem> list = upload.parseRequest(request);
// 遍历: 得到每一个上传的数据
for (FileItem item: list){
// 判断:普通文本数据
if (item.isFormField()){
// 普通文本数据
String fieldName = item.getFieldName(); // 表单元素名称
String content = item.getString(); // 表单元素名称, 对应的数据
//item.getString("UTF-8"); 指定编码
System.out.println(fieldName + " " + content);
}
// 上传文件(文件流) ----> 上传到upload目录下
else {
// 普通文本数据
String fieldName = item.getFieldName(); // 表单元素名称
String name = item.getName(); // 文件名
String content = item.getString(); // 表单元素名称, 对应的数据
String type = item.getContentType(); // 文件类型
InputStream in = item.getInputStream(); // 上传文件流 /*
* 四、文件名重名
* 对于不同用户readme.txt文件,不希望覆盖!
* 后台处理: 给用户添加一个唯一标记!
*/
// a. 随机生成一个唯一标记
String id = UUID.randomUUID().toString();
// b. 与文件名拼接
name = id +"#"+ name; // 获取上传基路径
String path = getServletContext().getRealPath("/upload");
// 创建目标文件
File file = new File(path,name); // 工具类,文件上传
item.write(file);
item.delete(); //删除系统产生的临时文件 System.out.println();
} } }
else {
System.out.println("当前表单不是文件上传表单,处理失败!");
}
} catch (Exception e) {
e.printStackTrace();
} }

文件上传与下载,完整案例:

步骤:

1. 文件上传

2. 列表下载

Index.jsp
<body>
<a href="${pageContext.request.contextPath }/upload.jsp">文件上传</a> &nbsp;&nbsp;&nbsp;
<a href="${pageContext.request.contextPath }/fileServlet?method=downList">文件下载</a> </body>
Upload.jsp
<body>
<form name="frm_test" action="${pageContext.request.contextPath }/fileServlet?method=upload" method="post" enctype="multipart/form-data">
<%--<input type="hidden" name="method" value="upload">--%> 用户名:<input type="text" name="userName"> <br/>
文件: <input type="file" name="file_img"> <br/> <input type="submit" value="提交">
</form>
</body>
Downlist.jsp
<body>
<table border="1" align="center">
<tr>
<th>序号</th>
<th>文件名</th>
<th>操作</th>
</tr>
<c:forEach var="en" items="${requestScope.fileNames}" varStatus="vs">
<tr>
<td>${vs.count }</td>
<td>${en.value }</td>
<td>
<%--<a href="${pageContext.request.contextPath }/fileServlet?method=down&..">下载</a>--%>
<!-- 构建一个地址 -->
<c:url var="url" value="fileServlet">
<c:param name="method" value="down"></c:param>
<c:param name="fileName" value="${en.key}"></c:param>
</c:url>
<!-- 使用上面地址 -->
<a href="${url }">下载</a>
</td>
</tr>
</c:forEach>
</table>
</body>
FileServlet.java

/**
* 处理文件上传与下载
* @author Jie.Yuan
*
*/
public class FileServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { // 获取请求参数: 区分不同的操作类型
String method = request.getParameter("method");
if ("upload".equals(method)) {
// 上传
upload(request,response);
} else if ("downList".equals(method)) {
// 进入下载列表
downList(request,response);
} else if ("down".equals(method)) {
// 下载
down(request,response);
}
} /**
* 1. 上传
*/
private void upload(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { try {
// 1. 创建工厂对象
FileItemFactory factory = new DiskFileItemFactory();
// 2. 文件上传核心工具类
ServletFileUpload upload = new ServletFileUpload(factory);
// 设置大小限制参数
upload.setFileSizeMax(10*1024*1024); // 单个文件大小限制
upload.setSizeMax(50*1024*1024); // 总文件大小限制
upload.setHeaderEncoding("UTF-8"); // 对中文文件编码处理 // 判断
if (upload.isMultipartContent(request)) {
// 3. 把请求数据转换为list集合
List<FileItem> list = upload.parseRequest(request);
// 遍历
for (FileItem item : list){
// 判断:普通文本数据
if (item.isFormField()){
// 获取名称
String name = item.getFieldName();
// 获取值
String value = item.getString();
System.out.println(value);
}
// 文件表单项
else {
/******** 文件上传 ***********/
// a. 获取文件名称
String name = item.getName();
// ----处理上传文件名重名问题----
// a1. 先得到唯一标记
String id = UUID.randomUUID().toString();
// a2. 拼接文件名
name = id + "#" + name; // b. 得到上传目录
String basePath = getServletContext().getRealPath("/upload");
// c. 创建要上传的文件对象
File file = new File(basePath,name);
// d. 上传
item.write(file);
item.delete(); // 删除组件运行时产生的临时文件
}
}
}
} catch (Exception e) {
e.printStackTrace();
} } /**
* 2. 进入下载列表
*/
private void downList(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { // 实现思路:先获取upload目录下所有文件的文件名,再保存;跳转到down.jsp列表展示 //1. 初始化map集合Map<包含唯一标记的文件名, 简短文件名> ;
Map<String,String> fileNames = new HashMap<String,String>(); //2. 获取上传目录,及其下所有的文件的文件名
String bathPath = getServletContext().getRealPath("/upload");
// 目录
File file = new File(bathPath);
// 目录下,所有文件名
String list[] = file.list();
// 遍历,封装
if (list != null && list.length > 0){
for (int i=0; i<list.length; i++){
// 全名
String fileName = list[i];
// 短名
String shortName = fileName.substring(fileName.lastIndexOf("#")+1);
// 封装
fileNames.put(fileName, shortName);
}
} // 3. 保存到request域
request.setAttribute("fileNames", fileNames);
// 4. 转发
request.getRequestDispatcher("/downlist.jsp").forward(request, response); } /**
* 3. 处理下载
*/
private void down(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { // 获取用户下载的文件名称(url地址后追加数据,get)
String fileName = request.getParameter("fileName");
fileName = new String(fileName.getBytes("ISO8859-1"),"UTF-8"); // 先获取上传目录路径
String basePath = getServletContext().getRealPath("/upload");
// 获取一个文件流
InputStream in = new FileInputStream(new File(basePath,fileName)); // 如果文件名是中文,需要进行url编码
fileName = URLEncoder.encode(fileName, "UTF-8");
// 设置下载的响应头
response.setHeader("content-disposition", "attachment;fileName=" + fileName); // 获取response字节流
OutputStream out = response.getOutputStream();
byte[] b = new byte[1024];
int len = -1;
while ((len = in.read(b)) != -1){
out.write(b, 0, len);
}
// 关闭
out.close();
in.close(); } public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
} }

file upload download的更多相关文章

  1. Pikachu-File Inclusion, Unsafe file download & Unsafe file upload

    Pikachu-File Inclusion, Unsafe file download & Unsafe file upload 文件包含漏洞 File Inclusion(文件包含漏洞)概 ...

  2. upload&&download

    package am.demo;  import java.io.File;  import java.io.IOException;  import java.util.Iterator;  imp ...

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

    jQuery File Upload 的 GitHub 地址:https://github.com/blueimp/jQuery-File-Upload 插件描述:jQuery File Upload ...

  4. 【转发】Html5 File Upload with Progress

    Html5 File Upload with Progress               Posted by Shiv Kumar on 25th September, 2010Senior Sof ...

  5. PhoneGap奇怪的现象:File FileTransfer download, 手机相册检测不到下载下来的图片(解决)

    我有个从服务器下载相片的功能在使用 File FileTransfer download api时,碰到了很奇怪的现象:图片已经从服务器里下载了,手机文件夹里也可以看到下载过来的图片,但是我的手机相册 ...

  6. jQuery File Upload blueimp with struts2 简单试用

    Official Site的话随便搜索就可以去了 另外新版PHP似乎都有问题  虽然图片都可以上传  但是response报错  我下载的是8.8.7木有问题   但是8.8.7版本结合修改main. ...

  7. jQuery File Upload 图片上传解决方案兼容IE6+

    1.下载:https://github.com/blueimp/jQuery-File-Upload 2.命令: npm install bower install ================= ...

  8. jQuery file upload cropper的 click .preview事件没有绑定成功

    测试 修改https://github.com/tkvw/jQuery-File-Upload/blob/master/basic-plus.html var node = $('<p id=& ...

  9. jQuery File Upload done函数没有返回

    最近在使用jQuery File Upload 上传图片时发现一个问题,发现done函数没有callback,经过一番折腾,找到问题原因,是由于dataType: ‘json’造成的,改为autoUp ...

随机推荐

  1. Mac下Intellij IDea发布Java Web项目详解五 开始测试

    测试前准备工作目录 Mac下Intellij IDea发布Web项目详解一 Mac下Intellij IDea发布Java Web项目(适合第一次配置Tomcat的家伙们)详解二 Mac下Intell ...

  2. <离散数学>学习笔记1--逻辑和证明

    今天开始离散数学的自学旅程. 主题:逻辑和证明 逻辑规则给出数学语句的准确含义.逻辑对计算机科学有着重要作用.为了理解数学,我么必须理解正确的数学论证是由什么组成的.只要证明一个数学语句是真的,我们就 ...

  3. Java 热部署深入探索

    简介 在 Java 开发领域,热部署一直是一个难以解决的问题,目前的 Java 虚拟机只能实现方法体的修改热部署,对于整个类的结构修改,仍然需要重启虚拟机,对类重新加载才能完成更新操作.对于某些大型的 ...

  4. jiaocheng https://github.com/CarpenterLee/JCFInternals

    https://github.com/CarpenterLee/JCFInternals

  5. Android三种实现自定义ProgressBar的方式介绍

    一.通过动画实现 定义res/anim/loading.xml如下: View Row Code<?xml version="1.0" encoding="UTF- ...

  6. 论C++与三国

    Scott Meyers曾说过,C++语言是一个语言联邦.C++包含面向过程,面向对象,泛型编程编程思想.现在C++11有加了一堆新特性,语言联邦更为庞大. 程序员们.常常挑起语言之争,甚至连大师级人 ...

  7. 文件标准IO的mode

    1. r / r+ 是不创建文件,前者只读,后者可读写 2. w / w+ 是能创建文件,并且把文件置空,前者只写,后者可读写 3. a / a+ 是能创建文件,但不置空文件,在文件末尾写,前者只写, ...

  8. Eclipse混淆文件导入Android Studio Gradle编译报input jar file is specified twice

    Eclipse项目中的混淆配置文件 复制到AS中 在混淆的过程中提示如下错误     Error:Execution failed for task ':app:proguardDemoRelease ...

  9. android核心服务初探

    终于进入android学习的进阶阶段,第一个课题是android的核心服务.首先,让我们来认识一下核心服务. android核心服务与app服务有所区别.app服务继承自Service基类,在app运 ...

  10. Linux基础系列—Linux体系结构和Linux内核结构

    /** ****************************************************************************** * @author    暴走的小 ...