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

The method of uploading Files by java

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

Share

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

This article introduces the relevant knowledge of "the method of java to achieve file upload". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

If we want to upload files in java development tools, we need to rely on the Commons-io.jar and Commons-fileupload.jar packages organized by Apache.

1. To create a new project, the editor here names him fileupdate. You can name it according to your own idea, and put the rack package under the WEB-INF/lib folder in our project, as shown below:

The 2.index.jsp page code is as follows:

File upload name:

Upload files:

The message.jsp code is as follows:

Upload file successfully! upload file successfully!

3.UploadSerlvet Code:

Import java.io.File;import java.io.IOException;import java.io.PrintWriter;import java.util.ArrayList;import java.util.List;import java.util.UUID;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.commons.fileupload.FileItem;import org.apache.commons.fileupload.FileUploadException;import org.apache.commons.fileupload.disk.DiskFileItemFactory;import org.apache.commons.fileupload.servlet.ServletFileUpload Import org.apache.commons.io.FileUtils;public class UploadServlet extends HttpServlet {public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doPost (request, response);} public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType ("text/html") PrintWriter out = response.getWriter (); DiskFileItemFactory sf= new DiskFileItemFactory (); / / instantiated disk by file list factory String path = request.getRealPath ("/ upload"); / / get the directory where the uploaded files are stored sf.setRepository (new File (path)); / / set the file storage directory sf.setSizeThreshold (1024x1024) / / set the file upload to be less than 1m and put it in memory String rename = ""; / / the newly generated file name String fileName = ""; / / the original file name String name = ""; / / the ordinary field field / / the servletupload file upload class ServletFileUpload sfu = new ServletFileUpload (sf) obtained from the factory Try {List lst = sfu.parseRequest (request) / / get all the elements in request for (FileItem fileItem: lst) {if (fileItem.isFormField ()) {if ("name" .equals (fileItem.getFieldName () {name = fileItem.getString ("UTF-8") }} else {/ / get the file name fileName = fileItem.getName () FileName = fileName.substring (fileName.lastIndexOf ("\") + 1); String houzhui = fileName.substring (fileName.lastIndexOf (".")); rename = UUID.randomUUID () + houzhui FileItem.write (new File (path, rename));}} catch (Exception e) {/ / TODO Auto-generated catch block e.printStackTrace () } response.sendRedirect ("message.jsp"); out.flush (); out.close ();}}

When we complete the creation, we usually automatically configure the relevant information in web.xml.

This is the end of the content of "java method for uploading files". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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