一.手动实现文件上传
1.指定表单类型为文件上传表单
2.提交方式必须为:POST
<form enctype="multipart/form-data" method="post" action="${pageContext.request.ContextPath}/test">
用户名:<input type="text" name="username"/>文件:<input type="file" name="file"> </form>
3.servlet代码
package com.cx.fileUpload; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; /** * Created by cxspace on 16-7-7. */ public class TestServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取表单数据 InputStream in = request.getInputStream(); //转换流 InputStreamReader inReader = new InputStreamReader(in); //缓冲流 BufferedReader reader = new BufferedReader(inReader); String str = null; while ((str=reader.readLine())!=null){ System.out.println(str); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } }
二.使用FileUpload组件
apache提供的开源项目
1.引入.jar包
2.写处理代码
package com.cx.fileUpload; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileItemFactory; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.InputStream; import java.util.List; /** * Created by cxspace on 16-7-7. */ @WebServlet(name = "FileUpload") public class FileUpload extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.创建文件上传工厂类 FileItemFactory fac = new DiskFileItemFactory(); //2.创建文件上传核心对象 ServletFileUpload upload = new ServletFileUpload(fac); //3.判断当前是否为文件上传表单 if (upload.isMultipartContent(request)){ //把请求数据转换为FileItem集合 try { List<FileItem> list = upload.parseRequest(request); for (FileItem item : list){ if (item.isFormField()) { // 普通表单 String fieldName = item.getFieldName(); //文本框名称 String content = item.getString(); //文本框值 } else { String name = item.getName(); //文件名 String contentType = item.getContentType(); //文件类型 InputStream in = item.getInputStream(); //文件流 System.out.println(name+"-"+contentType); //文件上传表单 } } } catch (FileUploadException e) { System.out.println("当前表单不是文件上传表单"); e.printStackTrace(); } } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } }
3.分析api
|-FileItemFactory 文件上传工厂类
fac.setRepository(); //设置临时目录
|-ServletFileUpload 文件上传核心类,可以获取所有的fileitem
List<FileItem> parseRequest(request); 拿到文件对象集合
isMultipartContent(request); 判断表单类型
upload.setFileSizeMax(10); 设置单个文件最大大小
upload.setSizeMax(100); 设置总文件最大大小 upload.setHeaderEncoding; 设置上传文件名的编码
|-FileItem
item.getFieldName(); 获取上传表单元素名称
item.getString(); 获取上传数据
item.getSring("UTF-8") 获取上传文件数据,处理中文
item.getContentType(); 获取上传文件类型[文件项]
item.getInputStream(); 获取文件流[文件项目]
item.getName(); 获取文件名
item.write(file); 写文件
item.delete();删除临时文件
文件下载核心代码
String fileName = request.getParameter("filename"); fileName = new String(fileName.getBytes("ISO-8859-1"),"UTF-8"); String basePath = getServletContext().getRealPath("/upload"); InputStream in = new FileInputStream(new FIle(basePath,fileName)); //设置下载相应头response.setHeader("content-disposition","attachment;fileName="+fileName); 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();
时间: 2024-11-20 19:49:32