资源简介
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\.myme
文件 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.xm
文件 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\me
文件 36 2016-09-26 09:27 Day01_FileUpload\WebRoot\me
目录 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.xm
文件 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个文件信息
相关资源
- jspsmartupload中文版
- 文件上传并读取plupload与java后台的整
- UploadController
- commons-fileupload-1.2.2.jar和commons-io-2.4.
- webupload Java版轻松上传4个G文件
- jspsmartuploadUTF-8.jar
- Uploadify HTML5 收费版本($5) UploadFiv
- commons-fileupload.jar和commons-io.jar
- java文件上传案例
- commons-fileupload-1.2.2.jar和commons-io.jar
- 基于java的uploadify上传demo
- JSP使用C标签时必须引入的jar包,jst
- DiskFileItemFactory需要的jar包commons-file
- commons-fileupload-1.3.3.jar commons-io-2.5.ja
- commons-fileupload-1.2.1.jar和commons-io-1.3.
- 仿163网盘无刷新文件上传 for Jsp_file
- jsp 多文件上传(zyupload)
- SWFUpload实现多上传及上传进度显示
- 文件socket上传okhttpUpLoader
- java/jsp 上传+图片预览
- uploadify html5版本插件
- Uploadify和java后台实现多图上传
- 最全的common-FileUpload.jar包
- jspsmartupload组件
- java 批量上传文件带进度条,支持fl
- jsp 上传文件
评论
共有 条评论