Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the function of uploading and downloading files by Java

Shulou Source: shulou.com Published: 2022-06-01 12:49:56 09月28日 Update

This article mainly explains the "Java how to achieve file upload and download function", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Java how to achieve file upload and download function" bar!

Step 1: guide the package

Import two dependent packages, commons-fileupload-1.3.3.jar and commons-io-2.4.jar

Step 2: write the front-end page

1. Submit the page index.jsp

Insert title here avatar:

2. Result.jsp of the result page

Insert title here

xx

Step 3: write upload and download code

1. Upload image fileUpload.java

Package cn.yz123123.controller;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import javax.servlet.ServletException;import javax.servlet.annotation.MultipartConfig;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.Part;@WebServlet ("/ fileUpload") @ MultipartConfigpublic class fileUpload extends HttpServlet {private static final long serialVersionUID = 1L Protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost (request, response);} protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {/ / String username = request.getParameter ("username"); Part part = request.getPart ("img"); / / get the real name of the file String header = part.getHeader ("content-disposition"); String realName = header.substring (header.indexOf ("filename=") + 10, header.length ()-1) / / get the file's own stream InputStream inputStream = part.getInputStream (); / / get the real path of file. If not, create String dir = request.getServletContext () .getRealPath ("/ file/"); File dirFile = new File (dir); / / just instantiate an object above, but not really create a folder if (! dirFile.exists ()) {dirFile.mkdirs () } / / create a file object and write it in the corresponding folder in the form of stream File file = new File (dir, realName); FileOutputStream fileOutputStream = new FileOutputStream (file); byte [] buf = new byte [1024]; int len; while ((len=inputStream.read (buf))! =-1) {fileOutputStream.write (buf, 0, len);} fileOutputStream.close (); inputStream.close () / / the following is test request.setAttribute ("src", request.getContextPath () + "/ file/" + realName); request.setAttribute ("filename", realName); request.getRequestDispatcher ("/ result.jsp") .forward (request, response);}}

2. Download picture fileDownload.java

Package cn.yz123123.controller;import java.io.FileInputStream;import java.io.IOException;import java.util.UUID;import javax.servlet.ServletException;import javax.servlet.ServletOutputStream;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;@WebServlet (urlPatterns = "/ download") public class fileDownload extends HttpServlet {@ Override protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {String filename = req.getParameter ("filename") / / get the real path to the file String filePath = req.getServletContext (). GetRealPath ("/ file/" + filename); FileInputStream fileInputStream = new FileInputStream (filePath); resp.setCharacterEncoding ("UTF-8"); resp.setHeader ("Content-Disposition", "attachment;filename=" + UUID.randomUUID () + filename); ServletOutputStream outputStream = resp.getOutputStream (); byte [] buf = new byte [1024]; int len; while ((len=fileInputStream.read (buf))! =-1) {outputStream.write (buf, 0, len) } outputStream.close (); fileInputStream.close ();} @ Override protected void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {super.doGet (req, resp) }} Thank you for your reading, the above is the content of "how to achieve the function of file upload and download by Java". After the study of this article, I believe you have a deeper understanding of how Java can achieve the function of file upload and download, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: File function page learning content picture object folder path two code front end just name avatar instance that is form idea situation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Shulou Information macOS Apple Huawei