• 大小: 217KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-05-16
  • 语言: Java
  • 标签: upload  

资源简介

JavaEE 文件上传代码示例

资源截图

代码片段和文件信息

package com.hello.web.servlet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

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.FileItemFactory;
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.FilenameUtils;

public class FileUploadServlet extends HttpServlet {

public void doGet(HttpServletRequest request HttpServletResponse response)
throws ServletException IOException {
if(ServletFileUpload.isMultipartContent(request)){
// Create a factory for disk-based file items
DiskFileItemFactory factory=new DiskFileItemFactory();
// Create a new file upload handler
ServletFileUpload upload=new ServletFileUpload(factory);
// 一个request里面的文件上传总共10M,还有一个是限制单个文件大小的,这是总文件大小。
upload.setSizeMax(1024*1024*10);
try {
List items=upload.parseRequest(request); 
for(FileItem item : items){
if(item.isFormField()){
// 普通表单
System.out.println(item.getFieldName()+“:“+item.getString());
}else{
// 文件表单
System.out.println(item.getFieldName()+“:“+item.getName()+“:“+item.getSize()+“:“+item.getContentType());
// 在用户无法访问的meta-INF里创建专门放文件的目录
SimpleDateFormat sdf=new SimpleDateFormat(“yyyyMMdd“);
String d=sdf.format(new Date());
String year=d.substring(0 4);
String month=d.substring(4 6);
String day=d.substring(6);
String path=this.getServletContext().getRealPath(“/WEB-INF/upload“+File.separator+year+File.separator+month+File.separator+day);
File directory=new File(path);
if(!directory.exists()){
directory.mkdirs();
}
String filename=item.getName();
if(filename!=null){
filename=FilenameUtils.getName(filename);
}
filename=UUID.randomUUID()+filename;
File file=new File(directoryfilename);
// 获得输入流
InputStream is=item.getInputStream();
FileOutputStream fos=new FileOutputStream(file);
int len=0;
byte[] b=new byte[1024];
while((len=is.read(b))!=-1){
fos.write(b 0 len);
}
}
}
} catch (FileUploadException e) {
e.printStackTrace();
}
}
}

public void doPost(HttpServletRequest request HttpServletResponse response)
throws ServletException IOException {
doGet(request response);
}

}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2016-09-26 09:27  Day01_FileUpload\
     文件         529  2016-09-26 09:27  Day01_FileUpload\.classpath
     目录           0  2016-09-26 13:52  Day01_FileUpload\.myeclipse\
     文件         321  2016-09-26 09:27  Day01_FileUpload\.mymetadata
     文件        1759  2016-09-26 09:32  Day01_FileUpload\.project
     目录           0  2016-09-26 09:27  Day01_FileUpload\.settings\
     文件         500  2016-09-26 09:27  Day01_FileUpload\.settings\.jsdtscope
     文件         395  2016-09-26 09:27  Day01_FileUpload\.settings\org.eclipse.jdt.core.prefs
     文件         486  2016-09-26 09:27  Day01_FileUpload\.settings\org.eclipse.wst.common.component
     文件         252  2016-09-26 09:27  Day01_FileUpload\.settings\org.eclipse.wst.common.project.facet.core.xml
     文件          49  2016-09-26 09:27  Day01_FileUpload\.settings\org.eclipse.wst.jsdt.ui.superType.container
     文件           6  2016-09-26 09:27  Day01_FileUpload\.settings\org.eclipse.wst.jsdt.ui.superType.name
     目录           0  2016-09-26 09:27  Day01_FileUpload\WebRoot\
     目录           0  2016-09-26 09:27  Day01_FileUpload\WebRoot\meta-INF\
     文件          36  2016-09-26 09:27  Day01_FileUpload\WebRoot\meta-INF\MANIFEST.MF
     目录           0  2016-09-26 09:27  Day01_FileUpload\WebRoot\WEB-INF\
     目录           0  2016-09-26 09:34  Day01_FileUpload\WebRoot\WEB-INF\classes\
     目录           0  2016-09-26 09:34  Day01_FileUpload\WebRoot\WEB-INF\classes\com\
     目录           0  2016-09-26 09:34  Day01_FileUpload\WebRoot\WEB-INF\classes\com\hello\
     目录           0  2016-09-26 09:34  Day01_FileUpload\WebRoot\WEB-INF\classes\com\hello\web\
     目录           0  2016-09-26 09:34  Day01_FileUpload\WebRoot\WEB-INF\classes\com\hello\web\servlet\
     文件        4317  2016-09-26 13:40  Day01_FileUpload\WebRoot\WEB-INF\classes\com\hello\web\servlet\FileUploadServlet.class
     目录           0  2016-09-26 09:34  Day01_FileUpload\WebRoot\WEB-INF\lib\
     文件       59590  2016-09-26 09:34  Day01_FileUpload\WebRoot\WEB-INF\lib\commons-fileupload-1.2.2.jar
     文件      173587  2016-09-26 09:28  Day01_FileUpload\WebRoot\WEB-INF\lib\commons-io-2.2.jar
     文件         715  2016-09-26 09:29  Day01_FileUpload\WebRoot\WEB-INF\web.xml
     文件        1095  2016-09-26 09:32  Day01_FileUpload\WebRoot\index.jsp
     目录           0  2016-09-26 09:29  Day01_FileUpload\src\
     目录           0  2016-09-26 09:29  Day01_FileUpload\src\com\
     目录           0  2016-09-26 09:29  Day01_FileUpload\src\com\hello\
     目录           0  2016-09-26 09:29  Day01_FileUpload\src\com\hello\web\
............此处省略2个文件信息

评论

共有 条评论