标签:java上传文件 commons-fileupload multipart/form-data file
文件上传
导入commons-fileupload-1.2.2.jar
和commons-io-2.4.jar
1.设置jsp文件内的form表单
(1)method="post"方式提交;
(2)编码类型enctype="multipart/form-data";
(3)文件上传框<input type="file" name="file"/>必须设置name属性。
2.设置servlet页面
(1)设置request请求编码为"UTF-8";
(2)通过DiskFileItemFactory创建制造工厂
创建文件上传的核心类ServletFileUpload
(3)用ServletFileUpload.isMultipartContent(request)
判断request请求的编码类型是否是文件上传类型multipart/form-data
返回boolean值。
(4)获取上传文件的绝对路径。
(5)如果返回值是true,也就是说是文件上传编码类型,
则用得到的文件上传的核心类ServletFileUpload解析request
创建的文件上传类名.parseRequest(request)
(6)如果获取的集合不为空值,则进行遍历并且判断遍历的文件类型是否是普通输入框;
若是,则用InputStream流对文件进行读取,这是要注意浏览器获取文件名的兼容性,
为防止文件名获取错误,要对得到的文件名进行substring分割,
再用FileOutputStream流和buffer方法,将文件写入,关闭流,文件上传成功!
upload.jsp
<form action="${pageContext.request.contextPath}/uploadFileServlet" method="post" enctype="multipart/form-data"> 用户名:<input name="uname"/> 文件上传:<input type="file" name="file"/> <input type="submit" value="上传"/> </form>
uploadFileServlet.java
import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; 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.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; public class uploadFileServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); //获取DiskFileItemFactory DiskFileItemFactory dfactory = new DiskFileItemFactory(); //2.获取文件上传的核心类ServletFileUpload ServletFileUpload sfup = new ServletFileUpload(dfactory); //3判断请求是否是multipart/form-data boolean flag = ServletFileUpload.isMultipartContent(request); //获取上传文件的绝对路径 String realPath = this.getServletContext().getRealPath("files"); System.out.println(this.getServletContext()); System.out.println(realPath); if(flag){ try { //解析request List<FileItem> items = sfup.parseRequest(request); if(items!=null){ //遍历 for (FileItem fileItem : items) { //判断如果是普通输入框 if(fileItem.isFormField()){ String fieldName = fileItem.getFieldName(); String value = fileItem.getString(); System.out.println(fieldName+"---"+value); }else{ //就是文件上传input System.out.println("文件上传开始。。"); InputStream inputStream = fileItem.getInputStream(); //获取上传文件的文件名 String filename = fileItem.getName(); filename=filename.substring(filename.lastIndexOf("\\")+1); System.out.println("文件名:"+filename); FileOutputStream fos = new FileOutputStream(realPath+"\\"+filename); byte[] buffer = new byte[1024]; int len = 0; while((len=inputStream.read(buffer))!=-1){ fos.write(buffer, 0, len); } inputStream.close(); fos.close(); System.out.println("文件上传成功!"); } } } } catch (FileUploadException e) { e.printStackTrace(); } } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }
标签:java上传文件 commons-fileupload multipart/form-data file
原文地址:http://11836266.blog.51cto.com/11826266/1871233