javapart上传⽂件代码_Java⽂件上传下载、邮件收发实例代码⽂件上传下载
前台:
1. 提交⽅式:post
2. 表单中有⽂件上传的表单项:
3. 指定表单类型:
默认类型:enctype="application/x-www-form-urlencoded"
⽂件上传类型:multipart/form-data
FileUpload
⽂件上传功能开发中⽐较常⽤,apache也提供了⽂件上传组件!
FileUpload组件:
1. 下载源码
2. 项⽬中引⼊jar⽂件
commons-fileupload-1.2.1.jar 【⽂件上传组件核⼼jar包】
commons-io-1.4.jar 【封装了对⽂件处理的相关⼯具类】
使⽤:
public class UploadServlet extends HttpServlet {
// upload⽬录,保存上传的资源
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/*********⽂件上传组件: 处理⽂件上传************/
connect下载
try {
// 1. ⽂件上传⼯⼚
FileItemFactory factory = new DiskFileItemFactory();
// 2. 创建⽂件上传核⼼⼯具类
ServletFileUpload upload = new ServletFileUpload(factory);
// ⼀、设置单个⽂件允许的最⼤的⼤⼩: 30M
upload.setFileSizeMax(30*1024*1024);
// ⼆、设置⽂件上传表单允许的总⼤⼩: 80M
upload.setSizeMax(80*1024*1024);
// 三、 设置上传表单⽂件名的编码
// 相当于:request.setCharacterEncoding("UTF-8");
upload.setHeaderEncoding("UTF-8");
// 3. 判断: 当前表单是否为⽂件上传表单
if (upload.isMultipartContent(request)){
// 4. 把请求数据转换为⼀个个FileItem对象,再⽤集合封装
List list = upload.parseRequest(request);
// 遍历: 得到每⼀个上传的数据
for (FileItem item: list){
// 判断:普通⽂本数据
if (item.isFormField()){
// 普通⽂本数据
String fieldName = FieldName(); // 表单元素名称
String content = String(); // 表单元素名称, 对应的数据//String("UTF-8"); 指定编码
System.out.println(fieldName + " " + content);
}
// 上传⽂件(⽂件流) ----> 上传到upload⽬录下
else {
// 普通⽂本数据
String fieldName = FieldName(); // 表单元素名称
String name = Name(); // ⽂件名
String content = String(); // 表单元素名称, 对应的数据String type = ContentType(); // ⽂件类型
InputStream in = InputStream(); // 上传⽂件流
/*
* 四、⽂件名重名
* 对于不同⽤户⽂件,不希望覆盖!
* 后台处理: 给⽤户添加⼀个唯⼀标记!
*/
// a. 随机⽣成⼀个唯⼀标记
String id = UUID.randomUUID().toString();
// b. 与⽂件名拼接
name = id +"#"+ name;
// 获取上传基路径
String path = getServletContext().getRealPath("/upload");
// 创建⽬标⽂件
File file = new File(path,name);
// ⼯具类,⽂件上传
item.write(file);
item.delete(); //删除系统产⽣的临时⽂件
System.out.println();
}
}
}
else {
System.out.println("当前表单不是⽂件上传表单,处理失败!");
}
} catch (Exception e) {
e.printStackTrace();
}
}
// ⼿动实现过程
private void upload(HttpServletRequest request) throws IOException, UnsupportedEncodingException {
/*
/***********⼿动获取⽂件上传表单数据************/
//1. 获取表单数据流
InputStream in = InputStream();
//2. 转换流
InputStreamReader inStream = new InputStreamReader(in, "UTF-8"); //3. 缓冲流
BufferedReader reader = new BufferedReader(inStream);
// 输出数据
String str = null;
while ((str = adLine()) != null) {
System.out.println(str);
}
// 关闭
reader.close();
inStream.close();
in.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
案例:
Index.jsp
Upload.jsp
--%>
⽤户名:
⽂件:
FileServlet.Java
/**
* 处理⽂件上传与下载
* @author Jie.Yuan
*
*/
public class FileServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取请求参数: 区分不同的操作类型
String method = Parameter("method");
if ("upload".equals(method)) {
// 上传
upload(request,response);
}
else if ("downList".equals(method)) {
// 进⼊下载列表
downList(request,response);
}
else if ("down".equals(method)) {
// 下载
down(request,response);
}
}
/**
* 1. 上传
*/
private void upload(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
// 1. 创建⼯⼚对象
FileItemFactory factory = new DiskFileItemFactory();
// 2. ⽂件上传核⼼⼯具类
ServletFileUpload upload = new ServletFileUpload(factory);
// 设置⼤⼩限制参数
upload.setFileSizeMax(10*1024*1024); // 单个⽂件⼤⼩限制
upload.setSizeMax(50*1024*1024); // 总⽂件⼤⼩限制
upload.setHeaderEncoding("UTF-8"); // 对中⽂⽂件编码处理
// 判断
if (upload.isMultipartContent(request)) {
// 3. 把请求数据转换为list集合
List list = upload.parseRequest(request);
// 遍历
for (FileItem item : list){
// 判断:普通⽂本数据
if (item.isFormField()){
// 获取名称
String name = FieldName();
// 获取值
String value = String();
System.out.println(value);
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。