使用ServletFileUpload实现上传
2014-04-29 22:18
429 查看
使用ServletFileUpload实现上传
转载自/article/9156547.html1.首先我们应该为上传的文件建一个存放的位置,一般位置分为临时和真是文件夹,那我们就需要获取这俩个文件夹的绝对路径,在servlet中我们可以这样做
[java]
view plaincopyprint?
ServletContext application = this.getServletContext();
String tempDirectory = application.getRealPath(Constant.TEMP_DIRECTORY) +
"/";
String realDirectory = application.getRealPath(Constant.REAL_DIRECTORY) +
"/";
ServletContext application = this.getServletContext(); String tempDirectory = application.getRealPath(Constant.TEMP_DIRECTORY) + "/"; String realDirectory = application.getRealPath(Constant.REAL_DIRECTORY) + "/";
然后建立文件工厂即仓库一个参数表示存放多大后flush,
[java]
view plaincopyprint?
FileItemFactory factory = new DiskFileItemFactory(Constant.SIZE_THRESHOLD,new File(tempDirectory));
ServletFileUpload upload = new ServletFileUpload(factory);
FileItemFactory factory = new DiskFileItemFactory(Constant.SIZE_THRESHOLD,new File(tempDirectory)); ServletFileUpload upload = new ServletFileUpload(factory);2.对上传的文件进行设定
[java]
view plaincopyprint?
upload.setSizeMax(500*1024*1024);//设置该次上传最大值为500M
upload.setSizeMax(500*1024*1024);//设置该次上传最大值为500M3,.解析请求正文,获取上传文件,不抛出异常则写入真是路径
[java]
view plaincopyprint?
List<FileItem> list = upload.parseRequest(request);
Iterator<FileItem> iter = list.iterator();
while (iter.hasNext()) {
FileItem item = iter.next();
//item.isFormField()用来判断当前对象是否是file表单域的数据 如果返回值是true说明不是 就是普通表单域
if(item.isFormField()){
System.out.println(
"普通表单域" +item.getFieldName());
System.out.println(item.getString("utf-8"));
}else{
//System.out.println("file表单域" + item.getFieldName());
/*
* 只有file表单域才将该对象中的内容写到真实文件夹中
*/
String lastpath = item.getName();//获取上传文件的名称
lastpath = lastpath.substring(lastpath.lastIndexOf("."));
String filename = UUID.randomUUID().toString().replace("-",
"") + lastpath;
item.write(new File(realDirectory+filename));
List<FileItem> list = upload.parseRequest(request); Iterator<FileItem> iter = list.iterator(); while (iter.hasNext()) { FileItem item = iter.next(); //item.isFormField()用来判断当前对象是否是file表单域的数据 如果返回值是true说明不是 就是普通表单域 if(item.isFormField()){ System.out.println( "普通表单域" +item.getFieldName()); System.out.println(item.getString("utf-8")); }else{ //System.out.println("file表单域" + item.getFieldName()); /* * 只有file表单域才将该对象中的内容写到真实文件夹中 */ String lastpath = item.getName();//获取上传文件的名称 lastpath = lastpath.substring(lastpath.lastIndexOf(".")); String filename = UUID.randomUUID().toString().replace("-", "") + lastpath; item.write(new File(realDirectory+filename));
相关文章推荐
- 使用common-fileUpload和 Spring中MultipartHttpServletRequest实现文件上传
- 使用ServletFileUpload实现上传
- 在Servlet中使用开源fileupload包实现文件上传功能 2010-08-05 23:38:25 标签:Servlet 文件上传 fileupload包 反编译 JD-GUI 原创作品,允许
- 使用ServletFileUpload实现上传
- 使用ServletFileUpload实现上传
- 使用servletfileupload实现表单文件和数据的一起上传
- 使用FileUpload实现Servlet的文件上传
- 使用ServletFileUpload实现上传
- 使用servlet实现文件上传(用common-fileUpload和common-io包)
- 使用FileUpload实现Servlet的文件上传
- Servlet使用commons-fileupload实现文件上传
- 使用servlet实现文件上传(用common-fileUpload和common-io包)
- 使用ServletFileUpload实现上传
- servlet中使用commons-fileupload实现图片上传
- 使用ServletFileUpload 实现文件上传 和获取文件信息
- 使用Servlet实现文件上传
- 在Servlet中使用开源fileupload包实现文件上传功能
- JAVA学习提高之---- FileUpload组件实现多文件上传(JSP+SERVLET)实现
- 使用fileupload组件实现文件上传
- JSP Servlet学习笔记——使用fileupload上传文件