新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Java怎么實(shí)現(xiàn)文件上傳和下載功能
Java實(shí)現(xiàn)文件上傳和下載功能的方法有很多,這里提供一種基于Spring Boot的簡(jiǎn)單實(shí)現(xiàn)方法。在Spring Boot中,可以使用MultipartFile類來(lái)處理文件上傳,使用HttpServletResponse類來(lái)處理文件下載。具體實(shí)現(xiàn)方法可以參考以下鏈接:
Java實(shí)現(xiàn)文件上傳功能
在Java中,我們可以使用Servlet和Apache的Commons FileUpload庫(kù)來(lái)實(shí)現(xiàn)文件上傳功能,以下是一個(gè)簡(jiǎn)單的示例:

成都創(chuàng)新互聯(lián)是網(wǎng)站建設(shè)技術(shù)企業(yè),為成都企業(yè)提供專業(yè)的成都做網(wǎng)站、網(wǎng)站建設(shè),網(wǎng)站設(shè)計(jì),網(wǎng)站制作,網(wǎng)站改版等技術(shù)服務(wù)。擁有10多年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制適合企業(yè)的網(wǎng)站。10多年品質(zhì),值得信賴!
1、我們需要在項(xiàng)目中引入Apache Commons FileUpload庫(kù),在pom.xml文件中添加以下依賴:
commons-fileupload commons-fileupload 1.4
2、創(chuàng)建一個(gè)HTML表單,用于上傳文件:
文件上傳
3、創(chuàng)建一個(gè)Servlet類,用于處理文件上傳請(qǐng)求:
import org.apache.commons.fileupload.FileItem;
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.File;
import java.io.IOException;
import java.util.List;
@WebServlet("/upload")
public class FileUploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 檢查是否為多媒體上傳
if (!ServletFileUpload.isMultipartContent(request)) {
// 如果不是則停止
PrintWriter writer = response.getWriter();
writer.println("Error: 表單必須包含 enctype=multipart/form-data");
writer.flush();
return;
}
// 配置上傳參數(shù)
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("UTF-8");
upload.setFileSizeMax(50 * 1024 * 1024); // 設(shè)置最大上傳文件大小為50MB
upload.setHeader("Connection", "Keep-Alive");
upload.setHeader("Charset", "UTF-8");
upload.setHeader("Content-Disposition", "attachment;filename="uploaded_file""); // 設(shè)置上傳文件名
upload.setThreadPoolSize(10); // 設(shè)置線程池大小為10個(gè)線程
upload.setProgressListener(new UploadProgressListener()); // 設(shè)置上傳進(jìn)度監(jiān)聽器
upload.start(); //開始上傳文件的處理流程,這里的參數(shù)是用來(lái)接收服務(wù)器返回的數(shù)據(jù)的,如果不需要處理服務(wù)器返回的數(shù)據(jù),可以不傳這個(gè)參數(shù),否則需要傳入一個(gè)實(shí)現(xiàn)了org.apache.commons.fileupload包中的IProgressListener接口的對(duì)象作為參數(shù),這個(gè)對(duì)象將會(huì)在每次文件塊傳輸時(shí)被調(diào)用,這樣就可以實(shí)時(shí)得到文件上傳進(jìn)度的信息了,然后通過(guò)list來(lái)獲取所有的文件項(xiàng)信息,最后將這些文件保存到指定位置,這里使用到了org.apache.commons.fileupload包中的FileItem類,該類代表了一個(gè)要被上傳的文件項(xiàng),它包含了文件的所有信息,如文件名、大小、類型等,然后根據(jù)這些信息創(chuàng)建一個(gè)File對(duì)象,并將其保存到指定位置,最后關(guān)閉流和連接即可,下面是代碼實(shí)現(xiàn): ```java @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //檢查是否為多媒體上傳 if (!ServletFileUpload.isMultipartContent(req)) { PrintWriter writer = resp.getWriter(); writer.println("Error: form must have enctype=multipart/form-data"); writer.flush(); return; } //配置上傳參數(shù) DiskFileItemFactory factory = new DiskFileItemFactory(); //創(chuàng)建一個(gè)對(duì)象用于存儲(chǔ)所有文件數(shù)據(jù) List formItems = upload.parseRequest(req); if (formItems != null && formItems.size() > 0) { //迭代表單數(shù)據(jù) for (int i = 0; i < formItems.size(); i++) { //獲取當(dāng)前表單項(xiàng) FileItem item = formItems.get(i); if (!item.isFormField()) { String name = new String(item.getName()); String filename = name + System.currentTimeMillis() + "_" + item.getContentType(); String filePath = getServletContext().getRealPath("/upload") + File.separator + filename; File storeFile = new File(filePath); //保存文件 item.write(storeFile); System.out.println("文件已保存:" + filePath); } else { String value = item.getString("value"); System.out.println("字段名:" + item.getFieldName() + ",值:" + value); //輸出表單字段的值 if (item != null && item instanceof org.apache.commons.fileupload.disk.DiskFileItem){ try{ ((org.apache.commons.fileupload.disk.DiskFileItem)item).delete();} catch (Exception e){}//刪除臨時(shí)文件 try{ ((org
網(wǎng)站標(biāo)題:Java怎么實(shí)現(xiàn)文件上傳和下載功能
文章網(wǎng)址:http://www.dlmjj.cn/article/dhgisos.html


咨詢
建站咨詢
