首页 > 代码库 > Servlet中的文件上传
Servlet中的文件上传
上传表单中的注意事项:
表单 method 属性应该设置为 POST 方法,不能使用 GET 方法
表单 enctype 属性应该设置为multipart/form-data
下面的实例是借助于commons-fileupload和commons-io相关包来实现的
import java.io.*; import java.util.*; import javax.servlet.ServletConfig; 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; import org.apache.commons.io.output.*; public class UploadServletextends HttpServlet { private boolean isMultipart; private String filePath; private int maxFileSize = 50 * 1024; private int maxMemSize = 4 * 1024; private File file ; public void init( ){ // 获取文件将被存储的位置 filePath = getServletContext().getInitParameter("file-upload"); } public void doPost(HttpServletRequestrequest, HttpServletResponse response) throws ServletException,java.io.IOException { // 检查我们有一个文件上传请求 isMultipart = ServletFileUpload.isMultipartContent(request); response.setContentType("text/html"); java.io.PrintWriter out =response.getWriter( ); if( !isMultipart ){ out.println("<html>"); out.println("<head>"); out.println("<title>Servletupload</title>"); out.println("</head>"); out.println("<body>"); out.println("<p>No fileuploaded</p>"); out.println("</body>"); out.println("</html>"); return; } DiskFileItemFactoryfactory = new DiskFileItemFactory(); //设置上传文件时用于临时存放文件的内存大小 factory.setSizeThreshold(maxMemSize); //设置存放临时文件的目录,这个目录需要事先建立好 factory.setRepository(new File("c:\\temp")); // 创建一个新的文件上传处理程序 ServletFileUploadupload = new ServletFileUpload(factory); // 允许上传的文件大小的最大值 upload.setSizeMax( maxFileSize ); try{ // 解析请求,获取文件项 List fileItems = upload.parseRequest(request); // 处理上传的文件项 Iterator i = fileItems.iterator(); out.println("<html>"); out.println("<head>"); out.println("<title>Servletupload</title>"); out.println("</head>"); out.println("<body>"); while ( i.hasNext () ) { FileItem fi = (FileItem)i.next(); if ( !fi.isFormField () ) //是文件域 { // 获取上传文件的参数 String fieldName = fi.getFieldName(); String fileName = fi.getName(); //文件的全路径 String contentType = fi.getContentType(); boolean isInMemory =fi.isInMemory(); long sizeInBytes = fi.getSize(); //得到去除路径的文件名 if(fileName.lastIndexOf("\\") >= 0 ){ //路径中包含“\”时 file = new File( filePath + fileName.substring( fileName.lastIndexOf("\\"))); }else{ //路径中不包含“\”时 file = new File( filePath + fileName.substring(fileName.lastIndexOf("\\")+1)) ; } fi.write( file ) ; // 写入文件 out.println("UploadedFilename: " + fileName + "<br>"); } } out.println("</body>"); out.println("</html>"); }catch(Exception ex) { System.out.println(ex); } } public void doGet(HttpServletRequestrequest, HttpServletResponseresponse) throws ServletException,java.io.IOException { throw new ServletException("GETmethod used with " + getClass( ).getName( )+":POST method required."); } }
Servlet中的文件上传
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。