javaWeb之文件上传和下载
发布人:shili8
发布时间:2024-12-22 12:42
阅读次数:0
**Java Web 之 文件上传和下载**
在 Java Web 开发中,文件上传和下载是常见的功能需求。文件上传涉及用户将本地文件上传到服务器端,而文件下载则相反,即从服务器端下载文件到客户端。
###1. 文件上传####1.1. 使用 Apache Commons FileUpload 库Apache Commons FileUpload 是一个用于 Java 的文件上传组件,可以方便地处理 HTTP 请求中的文件数据。下面是使用该库进行文件上传的示例代码:
javaimport 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. />import javax.servlet. />import javax.servlet. />import java.io.File; import java.io.IOException; import java.util.List; public class FileUploadServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 检查请求是否包含文件上传数据 if (ServletFileUpload.isMultipartContent(request)) { DiskFileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); Listitems = null; try { items = upload.parseRequest(request); } catch (Exception e) { // 处理异常 } if (items != null && !items.isEmpty()) { for (FileItem item : items) { if (!item.isFormField()) { String fileName = item.getName(); String fileFolder = getServletContext().getInitParameter("fileUploadFolder"); File uploadFolder = new File(fileFolder); if (!uploadFolder.exists()) { uploadFolder.mkdirs(); } File uploadedFile = new File(uploadFolder, fileName); try { item.write(uploadedFile); // 处理上传成功 } catch (Exception e) { // 处理异常 } } } } } } }
####1.2. 使用 Spring 框架Spring 框架提供了一个简单的文件上传功能,可以使用 `MultipartHttpServletRequest` 接口来处理 HTTP 请求中的文件数据。下面是使用 Spring 框架进行文件上传的示例代码:
javaimport org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.multipart.MultipartFile; import javax.servlet. />import java.io.File; import java.io.IOException; @Controllerpublic class FileUploadController { @RequestMapping("/file/upload") public String uploadFile(HttpServletRequest request, MultipartFile file) throws IOException { // 检查请求是否包含文件上传数据 if (request.getContentType() != null && request.getContentType().startsWith("multipart/form-data")) { String fileName = file.getName(); String fileFolder = getServletContext().getInitParameter("fileUploadFolder"); File uploadFolder = new File(fileFolder); if (!uploadFolder.exists()) { uploadFolder.mkdirs(); } File uploadedFile = new File(uploadFolder, fileName); try { file.transferTo(uploadedFile); // 处理上传成功 } catch (IOException e) { // 处理异常 } } return "redirect:/"; } }
###2. 文件下载####2.1. 使用 HttpServletResponse 对象可以使用 `HttpServletResponse` 对象来返回文件数据给客户端。下面是使用该对象进行文件下载的示例代码:
javaimport javax.servlet. />import javax.servlet. />import javax.servlet. /> public class FileDownloadServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 检查请求是否包含文件下载数据 String fileName = request.getParameter("fileName"); String fileFolder = getServletContext().getInitParameter("fileDownloadFolder"); File downloadFile = new File(fileFolder, fileName); if (downloadFile.exists()) { response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment; filename="" + fileName + """); try (InputStream inputStream = new FileInputStream(downloadFile)) { IOUtils.copy(inputStream, response.getOutputStream()); } catch (IOException e) { // 处理异常 } } } }
####2.2. 使用 Spring 框架Spring 框架提供了一个简单的文件下载功能,可以使用 `MultipartFile` 对象来返回文件数据给客户端。下面是使用 Spring 框架进行文件下载的示例代码:
javaimport org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.multipart.MultipartFile; import javax.servlet. />import java.io.File; import java.io.IOException; @Controllerpublic class FileDownloadController { @RequestMapping("/file/download") public String downloadFile(HttpServletRequest request, MultipartFile file) throws IOException { // 检查请求是否包含文件下载数据 if (request.getContentType() != null && request.getContentType().startsWith("multipart/form-data")) { String fileName = file.getName(); String fileFolder = getServletContext().getInitParameter("fileDownloadFolder"); File downloadFile = new File(fileFolder, fileName); try { file.transferTo(downloadFile); // 处理下载成功 } catch (IOException e) { // 处理异常 } } return "redirect:/"; } }
以上是 Java Web 之文件上传和下载的相关内容,包括使用 Apache Commons FileUpload 库、Spring 框架等进行文件上传和下载的示例代码。