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 use SpringMVC to receive file stream uploads and form parameters

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

Share

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

This article mainly introduces "how to use SpringMVC to receive file stream upload and form parameters" related knowledge, editor through the actual case to show you the operation process, the method of operation is simple and fast, practical, I hope that this "how to use SpringMVC to receive file stream upload and form parameters" article can help you solve the problem.

Receive file stream upload and form parameters

In SpringMVC, receiving a file stream is very simple, and we can write an interface to receive some files as well as form parameters.

The code reference is as follows:

JAVA server code / * receives file stream * * @ param request request * @ return OK * / @ RequestMapping (value = "/ receive/file", method = POST) public String receiveFile (HttpServletRequest request) {/ / convert to MultipartHttpServletRequest if (request instanceof MultipartHttpServletRequest) {MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request / / receive the file stream through the parameter name in the form (file.getInputStream () can be used to receive the input stream) MultipartFile file = multipartRequest.getFile ("file"); System.out.println ("name of uploaded file:" + file.getOriginalFilename ()); System.out.println ("size of uploaded file:" + file.getSize ()) / / receive other form parameters String name = multipartRequest.getParameter ("name"); String content = multipartRequest.getParameter ("content"); System.out.println ("name:" + name); System.out.println ("content:" + content); return "OK";} else {return "not MultipartHttpServletRequest" }} HTML page code submission request SpringMVC to receive file upload and process the file

Http client version 4.1.1

Spring version 3.2.11

In this example, I received the file and forwarded it to another machine. In fact, I already got the stream of the file when I received it. I want to save it locally, or I can make my own decision about the file analysis.

Springmvc configuration text/html Charset=UTF-8 true 0 UTF-8 zh_CN 0.# # # ignore controller code is as follows: @ RequestMapping (value = "/ uploadFile" Method = RequestMethod.POST) @ ResponseBodypublic String uploadFile (HttpServletRequest request) throws Exception {FileOutputStream fos = null InputStream in = null; String fileUrl = null; try {/ / initialize the current context to CommonsMutipartResolver (multipart parser) CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver (request.getSession (). GetServletContext ()); / / check if there is an enctype= "multipart/form-data" if (multipartResolver.isMultipart (request)) in form {/ / turn request into multipart request MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request / / get all the file names in multiRequest Iterator iterator = multiRequest.getFileNames (); while (iterator.hasNext ()) {MultipartFile multipartFile = multiRequest.getFile (iterator.next (). ToString ()); in = multipartFile.getInputStream (); File file = new File (multipartFile.getOriginalFilename ()); fos = new FileOutputStream (file) Byte [] buff = new byte [1024]; int len = 0; while ((len = in.read (buff)) > 0) {fos.write (buff, 0, len);} String uploadUrl = platformHttpsDomain + "/ uploadFile"; HttpPost post = new HttpPost (uploadUrl) HttpClient httpclient = new DefaultHttpClient (); MultipartEntity reqEntity = new MultipartEntity (); reqEntity.addPart ("file", new FileBody (file)); post.setEntity (reqEntity); HttpResponse response = httpclient.execute (post); int statusCode = response.getStatusLine (). GetStatusCode () If (statusCode = = HttpStatus.SC_OK) {fileUrl = EntityUtils.toString (response.getEntity ());} file.deleteOnExit ();} catch (Exception e) {e.printStackTrace () } finally {if (fos! = null) {try {fos.close ();} catch (IOException e) {}} if (in! = null) {try {in.close () } catch (IOException e) {}} return fileUrl;} about "how to use SpringMVC to receive file stream uploads and form parameters" ends here. Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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