Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to upload files in Servlet

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to upload files in Servlet". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to upload files in Servlet".

First of all, a simple understanding of Servlet uploading files.

Previously, Servlet itself does not provide direct support for file upload, which generally needs to be implemented using a third-party framework, so it is more troublesome.

However, Servlet3.0 later provided this feature, and it is very easy to use, so HTTPServletRequest provides two methods to parse the uploaded file from the request:

Part getPart (String name) / / get the files of the given name in the request Collection getParts () / / get all the files

Each of these files is represented by a javax.servlet.http.Part object, and this interface provides a simple way to deal with files, such as write (), delete (), so it is very easy to save uploaded files by combining HttpServletRequest and Part.

Part img = request.getPart ("img"); img.write ("root path +\ img.jpg")

Note: you can customize the upload operation with the @ MultipartConfig annotation, such as limiting the size of the uploaded file and the path to save the file. However, if the MIME type of the request is not multipart/form-data, you cannot use the above two methods, otherwise an exception will be thrown.

First of all, let's learn the steps of developing Servlet:

Front-end: if there is front-end content, you need to write another html file

Back end

First write the background Servlet code (that is, override the doGet and doPost methods)

Configuration project file, mapping file in web.xml

1. First write a page (front-end) html file

Upload files

two。 Write Servlet backend class code

Import javax.servlet.ServletException;import javax.servlet.annotation.MultipartConfig;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.Part;import java.io.IOException;import java.io.PrintWriter / / first, you must declare that the class is [not available] @ MultipartConfigpublic class FileServlet extends HttpServlet {@ Override / / because only post is needed to submit the form, so there is no need for the get method protected void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException. IOException {/ / first gets the stream information. What he returns is a part object Part part = stream ("img"). / / declare a save path where String filePath = "D:\ File\" is saved in the File file on disk D; / / the name of the saved file is the same as the name of the original file, and the original file name String fileName = part.getSubmittedFileName () is obtained by the method in part. / / through the write method, you can save the png file in any path. The parameter in write is the saved path part.write (filePath+fileName); / / then the response result resp.setContentType ("text/html"); resp.setCharacterEncoding ("utf-8"); PrintWriter writer = resp.getWriter (); writer.println ("uploaded successfully") are returned to the frontend. }}

3. Configure web.xml Mappin

There are problems with fileServlet FileServlet fileServlet / upload

So where is the problem?

/ / first get the stream information, what he returns is a part object Part part = req.getPart ("img"); / / his staff declares a save path, which is saved in the File file on disk D, String filePath = "D:\ File\" / / the name of the saved file here is the same as the name of the original file. Get the name of the original file String fileName = part.getSubmittedFileName () through the method in part; / / through the write method, you can save the png file in any path. The parameter in write is the path part.write (filePath+fileName) to be saved.

Note: actually, there is a problem here.

So how to improve it?

Method: use UUID as the file name

Code improvement: that is, change the fileName in the code to the following

/ / first you need a file name. Improve the name of the file name (to prevent duplicate names from overwriting the previous file) String fileName = UUID.randomUUID () .toString () + part.getSubmittedFileName () .substring (part.getSubmittedFileName () .indexOf (".")) Thank you for your reading, the above is the content of "how to upload files in Servlet". After the study of this article, I believe you have a deeper understanding of how to upload files in Servlet, 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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report