使用commons-fileupload实现上传
来源:互联网 发布:王思仪淘宝店 编辑:程序博客网 时间:2024/05/21 17:01
看了个视频
有apache组织提供的commons-fileupload ,实现上传
jsp页面,web.xml省略
servlet
package com.xie.fileupload;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
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.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class FileUpload extends HttpServlet ...{
@SuppressWarnings("unchecked")
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException ...{
DiskFileItemFactory factory = new DiskFileItemFactory();
String path = request.getRealPath("/upload");
factory.setRepository(new File(path));
factory.setSizeThreshold(1024 * 1024);
ServletFileUpload upload = new ServletFileUpload(factory);
try ...{
List<FileItem> list = upload.parseRequest(request);
for (FileItem fileItem : list) ...{
// 如果是表单信息
if (fileItem.isFormField()) ...{
String name = fileItem.getFieldName();// 获得表单中的名字
String value = fileItem.getString("gbk");
request.setAttribute(name, value);
} else ...{
String name = fileItem.getFieldName();
String value = fileItem.getName();// 获得从浏览器中取得的文件名字
int start = value.lastIndexOf("/");
String fileName = value.substring(start + 1); // 由于不同的浏览器可能取得的文件的名字不同,有的浏览器将整个路径取道,有的浏览器只取到文件名
request.setAttribute(name, fileName);// 形如c:document.....txt和1.txt这两种形式,因此需要做次操作
// 使用io进行文件的上传
/**//*
* OutputStream os = new FileOutputStream(new
* File(path,fileName));
*
* InputStream in = fileItem.getInputStream();
*
* byte[] buffer = new byte[400];
*
* int length = 0 ;
*
* while((length - in.read(buffer))>0) { os.write(buffer, 0,
* length); }
*
* os.close(); in.close();
*/
// 使用fileupload提供的上传
try ...{
fileItem.write(new File(path, fileName));
} catch (Exception e) ...{
e.printStackTrace();
}
}
}
} catch (FileUploadException e) ...{
e.printStackTrace();
}
request.getRequestDispatcher("/fileUpload/success.jsp").forward(
request, response);
}
}
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
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.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class FileUpload extends HttpServlet ...{
@SuppressWarnings("unchecked")
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException ...{
DiskFileItemFactory factory = new DiskFileItemFactory();
String path = request.getRealPath("/upload");
factory.setRepository(new File(path));
factory.setSizeThreshold(1024 * 1024);
ServletFileUpload upload = new ServletFileUpload(factory);
try ...{
List<FileItem> list = upload.parseRequest(request);
for (FileItem fileItem : list) ...{
// 如果是表单信息
if (fileItem.isFormField()) ...{
String name = fileItem.getFieldName();// 获得表单中的名字
String value = fileItem.getString("gbk");
request.setAttribute(name, value);
} else ...{
String name = fileItem.getFieldName();
String value = fileItem.getName();// 获得从浏览器中取得的文件名字
int start = value.lastIndexOf("/");
String fileName = value.substring(start + 1); // 由于不同的浏览器可能取得的文件的名字不同,有的浏览器将整个路径取道,有的浏览器只取到文件名
request.setAttribute(name, fileName);// 形如c:document.....txt和1.txt这两种形式,因此需要做次操作
// 使用io进行文件的上传
/**//*
* OutputStream os = new FileOutputStream(new
* File(path,fileName));
*
* InputStream in = fileItem.getInputStream();
*
* byte[] buffer = new byte[400];
*
* int length = 0 ;
*
* while((length - in.read(buffer))>0) { os.write(buffer, 0,
* length); }
*
* os.close(); in.close();
*/
// 使用fileupload提供的上传
try ...{
fileItem.write(new File(path, fileName));
} catch (Exception e) ...{
e.printStackTrace();
}
}
}
} catch (FileUploadException e) ...{
e.printStackTrace();
}
request.getRequestDispatcher("/fileUpload/success.jsp").forward(
request, response);
}
}
- 使用commons-fileupload实现上传
- 使用commons-fileupload上传简单实现
- 使用commons-fileupload实现文件上传
- 使用commons-fileupload实现文件上传
- 使用Commons-fileupload组件实现文件上传
- 使用Commons-fileupload组件实现文件上传
- JSP使用commons-fileupload实现文件上传
- Jsp 使用commons-fileupload实现文件上传
- 使用commons-fileupload组件实现文件上传
- 使用commons-fileupload实现文件的上传
- 使用Apache commons-fileupload实现文件上传
- Servlet使用commons-fileupload实现文件上传
- commons-fileupload 实现文件上传
- Commons-Fileupload 实现文件上传
- Commons-FileUpload实现文件上传
- commons-fileupload实现文件上传
- 使用commons fileupload 上传文件
- 使用 Commons-fileupload 上传文件
- cin cout输入输出pk printf scanf
- 我只能给大家提供一段不同的经历
- 打印数字环形图案
- vmstat用法
- Linux Tomcat安装
- 使用commons-fileupload实现上传
- 编程系列之:矩阵打印
- 福州大学acm
- T60笔记本下字体的美化
- 一个memset引发的血案
- .net 学习笔记
- 再收集几个Hash函数的实现
- 设备自身MAC地址
- Form2操作FORM1的方法