package com.xdl.controller;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
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;
/**
* Servlet implementation class FileUpLode
*/
@WebServlet("/fileUpLode.do")
public class FileUpLode extends HttpServlet {
private static final long serialVersionUID = 1L;
/** 文件上传*/
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 创建一个工厂类 也可以理解成第三方架包 固定写法 因为它底层就是这么写的- -!
DiskFileItemFactory dff= new DiskFileItemFactory();
//创建一个servlet的文件上传类 需要放入上面工厂类对象
ServletFileUpload sfu= new ServletFileUpload(dff);
try {
// 解析 request对象(页面上上传的东西不都是在request里面吗?) 得到一个FileItem类型的数组
List<FileItem> items = sfu.parseRequest(request);
//遍历items
for (FileItem Item : items) {
//.isFormField()如果是普通的文本 就走if
if(Item.isFormField()){
System.out.println(Item.getFieldName()+":"+Item.getString());
}else{
//否则就走else
System.out.println(Item.getFieldName()+":"+Item.getName());
//获取getRealPath("File")中File的绝对路径
String filepath=request.getServletContext().getRealPath("File");
//再凭拼接一个当前时间 区分上传的文件名字
String flag= new Date().getTime()+"";
filepath=filepath+"/"+flag+Item.getName();
System.out.println(filepath);
//构建一个文件对象
File file = new File(filepath);
//直接调用Item.write() 传入 构建的file 完成文件上传 总结 看不懂就copy
Item.write(file);
}
}
} catch (FileUploadException e) {
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
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;
/**
* Servlet implementation class FileUpLode
*/
@WebServlet("/fileUpLode.do")
public class FileUpLode extends HttpServlet {
private static final long serialVersionUID = 1L;
/** 文件上传*/
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 创建一个工厂类 也可以理解成第三方架包 固定写法 因为它底层就是这么写的- -!
DiskFileItemFactory dff= new DiskFileItemFactory();
//创建一个servlet的文件上传类 需要放入上面工厂类对象
ServletFileUpload sfu= new ServletFileUpload(dff);
try {
// 解析 request对象(页面上上传的东西不都是在request里面吗?) 得到一个FileItem类型的数组
List<FileItem> items = sfu.parseRequest(request);
//遍历items
for (FileItem Item : items) {
//.isFormField()如果是普通的文本 就走if
if(Item.isFormField()){
System.out.println(Item.getFieldName()+":"+Item.getString());
}else{
//否则就走else
System.out.println(Item.getFieldName()+":"+Item.getName());
//获取getRealPath("File")中File的绝对路径
String filepath=request.getServletContext().getRealPath("File");
//再凭拼接一个当前时间 区分上传的文件名字
String flag= new Date().getTime()+"";
filepath=filepath+"/"+flag+Item.getName();
System.out.println(filepath);
//构建一个文件对象
File file = new File(filepath);
//直接调用Item.write() 传入 构建的file 完成文件上传 总结 看不懂就copy
Item.write(file);
}
}
} catch (FileUploadException e) {
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}