JAVA上传文件比较与实例
编程技术  /  houtizong 发布于 3年前   88
<%@ page language="java" import="java.util.*" contentType="text/html;charset=gbk" pageEncoding="gbk"%><html> <body> <form action="MultipartTestServlet" enctype="multipart/form-data" method="post"> <input type="text" name="username" /><br /> <input type="file" name="myfile" /><br/> <input type="file" name="myfile" /><br/> <input type="submit" /> </form> <br/><br/><br/><br/> <form action="MultipartTestServlet2" enctype="multipart/form-data" method="post"> <input type="text" name="username" /><br /> <input type="file" name="myfile" /><br/> <input type="file" name="myfile" /><br/> <input type="submit" /> </form> <br/><br/><br/><br/> <form action="MultipartTestServlet3" enctype="multipart/form-data" method="post"> <input type="text" name="username" /><br /> <input type="file" name="myfile" /><br/> <input type="file" name="myfile" /><br/> <input type="submit" /> </form> </body></html>
package com.bug.servlet;import java.io.File;import java.io.IOException;import java.util.ArrayList;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.FileUpload;import org.apache.commons.fileupload.FileUploadException;import org.apache.commons.fileupload.RequestContext;import org.apache.commons.fileupload.disk.DiskFileItemFactory;import org.apache.commons.fileupload.servlet.ServletFileUpload;import org.apache.commons.fileupload.servlet.ServletRequestContext;public class MultipartTestServlet extends HttpServlet { public MultipartTestServlet() { super(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("gbk"); RequestContext requestContext = new ServletRequestContext(request); if(FileUpload.isMultipartContent(requestContext)){ DiskFileItemFactory factory = new DiskFileItemFactory(); factory.setRepository(new File("c:/tmp/")); ServletFileUpload upload = new ServletFileUpload(factory); //upload.setHeaderEncoding("gbk"); upload.setSizeMax(2000000); List items = new ArrayList(); try { items = upload.parseRequest(request); } catch (FileUploadException e1) { System.out.println("文件上传发生错误" + e1.getMessage()); } Iterator it = items.iterator(); while(it.hasNext()){ FileItem fileItem = (FileItem) it.next(); if(fileItem.isFormField()){ System.out.println(fileItem.getFieldName() + " " + fileItem.getName() + " " + new String(fileItem.getString().getBytes("iso8859-1"), "gbk")); }else{ System.out.println(fileItem.getFieldName() + " " + fileItem.getName() + " " + fileItem.isInMemory() + " " + fileItem.getContentType() + " " + fileItem.getSize()); if(fileItem.getName()!=null && fileItem.getSize()!=0){ File fullFile = new File(fileItem.getName()); File newFile = new File("c:/temp/" + fullFile.getName()); try { fileItem.write(newFile); } catch (Exception e) { e.printStackTrace(); } }else{ System.out.println("文件没有选择 或 文件内容为空"); } } } } }}
package com.bug.servlet;import java.io.IOException;import java.util.Enumeration;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.oreilly.servlet.MultipartRequest;import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;public class MultipartTestServlet2 extends HttpServlet { public MultipartTestServlet2() { super(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //request.setCharacterEncoding("gbk"); 不起作用 System.out.println("start "); MultipartRequest multi = new MultipartRequest(request, "c:/tmp/", 2*1024*1024, "gbk", new DefaultFileRenamePolicy()); System.out.println("start "); Enumeration filesName = multi.getFileNames(); Enumeration paramsName = multi.getParameterNames(); while(paramsName.hasMoreElements()){ String paramName = (String) paramsName.nextElement(); System.out.println(multi.getParameter(paramName)); } while(filesName.hasMoreElements()){ String fileName = (String) filesName.nextElement(); System.out.println(multi.getFilesystemName(fileName) + " " + multi.getOriginalFileName(fileName) + " " + multi.getContentType(fileName) + " "); if(multi.getFilesystemName(fileName)!=null && !multi.getFilesystemName(fileName).equals("")) System.out.println(multi.getFile(fileName).toURI()); } } }MultipartTestServlet3.javapackage com.bug.servlet;import java.io.File;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.oreilly.servlet.multipart.FilePart;import com.oreilly.servlet.multipart.MultipartParser;import com.oreilly.servlet.multipart.ParamPart;import com.oreilly.servlet.multipart.Part;public class MultipartTestServlet3 extends HttpServlet { public MultipartTestServlet3() { super(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { MultipartParser mp = new MultipartParser(request, 2*1024*1024, false, false, "gbk"); Part part; while ((part = mp.readNextPart()) != null) { String name = part.getName(); if (part.isParam()) { ParamPart paramPart = (ParamPart) part; String value = paramPart.getStringValue(); System.out.println("param: name=" + name + "; value=" + value); } else if (part.isFile()) { // it's a file part FilePart filePart = (FilePart) part; String fileName = filePart.getFileName(); if (fileName != null) { long size = filePart.writeTo(new File("c:/tmp/")); System.out.println("file: name=" + name + "; fileName=" + fileName + ", filePath=" + filePart.getFilePath() + ", contentType=" + filePart.getContentType() + ", size=" + size); } else { System.out.println("file: name=" + name + "; EMPTY"); } System.out.flush(); } } } }
<servlet> <servlet-name>MultipartTestServlet</servlet-name> <servlet-class>com.bug.servlet.MultipartTestServlet</servlet-class> </servlet> <servlet> <servlet-name>MultipartTestServlet2</servlet-name> <servlet-class>com.bug.servlet.MultipartTestServlet2</servlet-class> </servlet> <servlet> <servlet-name>MultipartTestServlet3</servlet-name> <servlet-class>com.bug.servlet.MultipartTestServlet3</servlet-class> </servlet> <servlet-mapping> <servlet-name>MultipartTestServlet</servlet-name> <url-pattern>/MultipartTestServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>MultipartTestServlet2</servlet-name> <url-pattern>/MultipartTestServlet2</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>MultipartTestServlet3</servlet-name> <url-pattern>/MultipartTestServlet3</url-pattern> </servlet-mapping>
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成
网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];
文章归档
文章标签
友情链接