package Demo_1;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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;
public class doFile extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String method = request.getParameter("method");
if(method.equals("upload")){
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setFileSizeMax(30*1024*1024);
upload.setSizeMax(100*1024*1024);
if(upload.isMultipartContent(request)){
//普通表单处理
try {
List<FileItem> list = upload.parseRequest(request);
for(FileItem item : list){
if(item.isFormField()){
String filedName = item.getFieldName();//文本框民称 jsp页面中name中的值
String content = item.getString("UTF-8");//文本框值
}else{
//处理文江上传
String id = UUID.randomUUID().toString();
//获取文件名字
String name = id+"#"+item.getName();
//获取上传的目录路径
String basePath = getServletContext().getRealPath("/upload");
File file = new File(basePath, name);
item.write(file);
item.delete();
}
}
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
System.out.println("不是表单,不处理");
}
}
if(method.equals("showList")){
Map<String,String> fileNames = new HashMap<String, String>();
String basePath = getServletContext().getRealPath("/upload");
File file = new File(basePath);
String list[] = file.list();
if(list!=null && list.length>0){
for(int i=0;i<list.length;i++){
//全部名称
String fileName = list[i];
String shortName = list[i].substring(fileName.indexOf("#")+1, fileName.length());
fileNames.put(fileName, shortName);
}
}
request.setAttribute("fileNames", fileNames);
request.getRequestDispatcher("/showList.jsp").forward(request, response);
}
if(method.equals("down")){
//获取用户下载文件的名称
String fileName = request.getParameter("fileName");
//改变文件名称的编码
fileName = new String(fileName.getBytes("ISO8859-1"),"UTF-8");
//先获取上传目录路径
String basePath = getServletContext().getRealPath("/upload");
//获取一个文件流
FileInputStream in = new FileInputStream(new File(basePath,fileName));
//如果文件名是中文,需要进行url编码
fileName = URLEncoder.encode(fileName, "utf-8");
//设置下载的响应头
response.setHeader("content-disposition", "attachment;fileName="+fileName);
//获取response字节流
OutputStream out = response.getOutputStream();
byte[] b = new byte[1024];
int len = -1;
while((len = in.read(b))!=-1){
out.write(b, 0, b.length);
}
out.close();
in.close();
}
}
}
完整文件上传和下载(Servlet部分)
最新推荐文章于 2024-04-10 10:21:29 发布