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

What if there is a 405 error in SpringMVC uploading files across servers?

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

Share

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

Editor to share with you what to do when there are 405 errors in SpringMVC cross-server uploading files. I believe most people don't know much about it, so share this article for your reference. I hope you will get a lot after reading this article. Let's learn about it together.

405 error occurred in SpringMVC uploading files across servers

Here is the code for the application server

Package com.itheima.controller; import com.sun.jersey.api.client.Client;import com.sun.jersey.api.client.WebResource;import org.apache.commons.fileupload.FileItem;import org.apache.commons.fileupload.disk.DiskFileItemFactory;import org.apache.commons.fileupload.servlet.ServletFileUpload;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest;import java.io.File;import java.util.List Import java.util.UUID; @ Controller@RequestMapping ("/ user") public class UserController {@ RequestMapping ("/ fileupload3") public String fileupload3 (MultipartFile upload) throws Exception {System.out.println ("Cross-server file upload...."); / / define the path to upload the file server String path = "http://localhost:9090/uploads/"; System.out.println (upload.getBytes ()) / / define the uploaded file entry / / get the name of the uploaded file String filename = upload.getOriginalFilename (); / / set the name of the file to a unique value, uuid String uuid = UUID.randomUUID (). ToString (). Replace ("-", "); filename = uuid +" _ "+ filename; / / create client object Client client = Client.create () / / connecting to the image server WebResource webResource = client.resource (path + filename); / / equivalent to creating a connection object / / uploading files by webResource.put (upload.getBytes ()); return "success" } / * SpringMVC file upload * @ return * / @ RequestMapping ("/ fileupload2") public String fileuoload2 (HttpServletRequest request, MultipartFile upload) throws Exception {System.out.println ("springmvc file upload...") / / use fileupload component to complete file upload / / location String path = request.getSession (). GetServletContext (). GetRealPath ("/ uploads/"); / / determine whether the path exists File file = new File (path); if (! file.exists ()) {/ / create the folder file.mkdirs () } / / description upload file entry / / get the name of the uploaded file String filename = upload.getOriginalFilename (); / / set the name of the file to a unique value, uuid String uuid = UUID.randomUUID (). ToString (). Replace ("-", "); filename = uuid+" _ "+ filename / / complete file upload upload.transferTo (new File (path,filename)); return "success";} / * File upload * @ return * / @ RequestMapping ("/ fileupload1") public String fileuoload1 (HttpServletRequest request) throws Exception {System.out.println ("File upload...") / / use fileupload component to complete file upload / / location String path = request.getSession (). GetServletContext (). GetRealPath ("/ uploads/"); / / determine whether the path exists File file = new File (path); if (! file.exists ()) {/ / create the folder file.mkdirs () } / / parse the request object to get the uploaded file entry DiskFileItemFactory factory = new DiskFileItemFactory (); ServletFileUpload upload = new ServletFileUpload (factory); / / parse request List items = upload.parseRequest (request) / / traversing for (FileItem item:items) {/ / to judge Whether the current item object is the uploaded file item if (item.isFormField ()) {/ / explain the normal table one-way} else {/ / explain the uploaded file item / / get the name of the uploaded file String filename = item.getName () / / set the file name to a unique value, uuid String uuid = UUID.randomUUID (). ToString (). Replace ("-", "); filename = uuid+" _ "+ filename; / / complete file upload item.write (new File (path,filename)) / / Delete the temporary file item.delete ();}} return "success";}}

Springmvc.xml

Success.jsp

Title uploaded file successfully

Web.xml

Archetype Created Web Application dispatcherServlet org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:springmvc.xml 1 dispatcherServlet / default org.apache.catalina.servlets.DefaultServlet debug 0 listings true 1 characterEncodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding UTF-8 characterEncodingFilter / *

Index.jsp

File upload traditional file upload select file:

SpringMVC File upload Select File:

Upload File selection across servers: view the value of request.getSession () .getServletContext () .getRealPath ("\ uploads\")

If you encounter an error report of 405 perfect put http://localhost:9090/uploads/.........

Just add the following code to the web.xml in the file server

Default org.apache.catalina.servlets.DefaultServlet debug 0 readonly false listings false 1 here comes the key point ~

In idea, springmvc uploads files across servers and reports 405 errors, just like correcting web.xml.

This problem is because the Tomcat of the file server you are using is deployed in exploded mode, and the web.xml under the modified Tomcat local conf has no effect on the exploded project. At this time, you should use the war package mode to deploy, the locally modified web.xml files continue to be modified, and the modified Application context is not /, but can be changed to: / + any file name.

Then redeploy the Tomcat server so that the error is no longer reported. (note to modify the file upload path in the code)

These are all the contents of the article "what to do if there are 405 errors in SpringMVC uploading files across servers". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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