Servlet實現文件的上傳與下載

前言:

文件的上傳和下載在日常開發中很是常見,那麼這一功能是如何實現的呢,下面我給大傢介紹一下

實現條件:

1、需要一個form標簽,method為post請求
2、form的encType屬性值為multipart/form-data
3、input標簽的type=file
4.需要的jar包()

工程目錄:

具體實現:

UploadServlet.java(上傳)

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;

public class UploadServlet extends HttpServlet {
 @Override
 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 //1.判斷上傳的數據是否是多段數據(隻有事多段的數據才是文件上傳的)
 if(FileUpload.isMultipartContent(req)){
  //創建FileItemFactory工廠實現類
  FileItemFactory fileItemFactory=new DiskFileItemFactory();
  //創建用於解析上傳數據的工具類FileUpload
 FileUpload fileUpload=new FileUpload(fileItemFactory);
  try {
  List<FileItem> list=fileUpload.parseRequest(req);
  //循環判斷每一個表單項是普通類型還是上傳的文件
  for(FileItem fileItem:list){
   if(fileItem.isFormField()){
   //普通表單項
   System.out.println("表單項的name屬性值:"+fileItem.getFieldName());
   //參數UTF-8解決亂碼問題
   System.out.println("表單項的value值:"+fileItem.getString("UTF-8"));
   }else{
   //上傳的文件
   System.out.println("表單項的name屬性值:"+fileItem.getFieldName());
   System.out.println("上傳的文件名:"+fileItem.getName());
   fileItem.write(new File("D:\\"+fileItem.getName()));
   }

  }
  } catch (Exception e) {
  e.printStackTrace();
  }
 }
 }
}

download.java(下載)

import org.apache.commons.io.IOUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;

public class download extends HttpServlet {
 @Override
 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 //1.獲取要下載的文件名
 String downloadfilename="1.jpg";
 //2.讀取下載的文件內容(通過ServletContext對象可以讀取)
 ServletContext servletContext=getServletContext();
 //獲取要下載的文件類型
 String mimetype=servletContext.getMimeType("/file/"+downloadfilename);
 //4.回傳前告訴客戶端回傳的類型
 resp.setContentType("mimetype");
 //5.告訴客戶端收到的數據是用來下載的(響應頭)
 //Content-Disposition 響應頭表示收到的數據怎麼處理
 //attachment表示附件,表示下載
 resp.setHeader("Content-Disposition","attachment;filename="+downloadfilename);
  //url編碼解決下載文件名亂碼問題(谷歌和IE)
 // resp.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode("中國.jpg","UTF-8"));

 InputStream resourceAsStream= servletContext.getResourceAsStream("/file/"+downloadfilename);
 //獲取響應的輸出流
 OutputStream outputStream=resp.getOutputStream();
 //3,把下載的文件傳回給客戶端
 //讀取輸入流的數據復制給輸出流,輸出給客戶端
 IOUtils.copy(resourceAsStream,outputStream);

 }
}

前端頁面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Title</title>
</head>
<body>
<form action="http://localhost:8080/upandload_war_exploded/uploadServlet" method="post" enctype="multipart/form-data" >
 用戶名:<input type="text" name="username"><br>
 頭像:<input type="file" name="photo"><br>
 <input type="submit" value="上傳">
</form>
</body>
</html>

成果展示:

文件的上傳

服務器接受上傳的文件

下載文件

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: