文件上传下载——servlet

fileupload 组件可以将所有的请求信息都解析成 FileIteam 对象,可以通过对 FileItem 对象的操作完成 上传,⾯向对象的思想。

package com.southwind.servlet;
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;
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 java.io.*;
import java.util.List;
@WebServlet("/upload")
public class UploadServlet extends HttpServlet {
 @Override
 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
 }
 @Override
 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// //通过输⼊流获取客户端传来的数据流
// InputStream inputStream = req.getInputStream();
// Reader reader = new InputStreamReader(inputStream);
// BufferedReader bufferedReader = new BufferedReader(reader);
// //通过输出流将数据流输出到本地硬盘
// //获取⽂件夹的绝对路径
// String path = req.getServletContext().getRealPath("file/copy.txt");
// OutputStream outputStream = new FileOutputStream(path);
// Writer writer = new OutputStreamWriter(outputStream);
// BufferedWriter bufferedWriter = new BufferedWriter(writer);
// String str = "";
// while((str = bufferedReader.readLine())!=null){
// System.out.println(str);
// bufferedWriter.write(str);
// }
// bufferedWriter.close();
// writer.close();
// outputStream.close();
// bufferedReader.close();
// reader.close();
// inputStream.close();
 try {
 DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();
 ServletFileUpload servletFileUpload = new
ServletFileUpload(fileItemFactory);
 List<FileItem> list = servletFileUpload.parseRequest(req);
 for(FileItem fileItem : list){
 if(fileItem.isFormField()){
 String name = fileItem.getFieldName();
 String value = fileItem.getString("UTF-8");
 System.out.println(name+":"+value);
 }else{
 String fileName = fileItem.getName();
 long size = fileItem.getSize();
 System.out.println(fileName+":"+size+"Byte");
 InputStream inputStream = fileItem.getInputStream();
// Reader reader = new InputStreamReader(inputStream);
// BufferedReader bufferedReader = new
BufferedReader(reader);
 String path =
req.getServletContext().getRealPath("file/"+fileName);
 OutputStream outputStream = new FileOutputStream(path);
// Writer writer = new OutputStreamWriter(outputStream);
// BufferedWriter bufferedWriter = new
BufferedWriter(writer);
 int temp = 0;
 while((temp = inputStream.read())!=-1){
 outputStream.write(temp);
 }
// bufferedWriter.close();
// writer.close();
 outputStream.close();
// bufferedReader.close();
// reader.close();
 inputStream.close();
 System.out.println("上传成功");
 }
 }
 } catch (FileUploadException e) {
 e.printStackTrace();
 }
 }
}

 

posted @ 2020-11-01 09:24  Gazikel  阅读(41)  评论(0编辑  收藏  举报