download:
1. Configure in spring-mvc (for downloading files below 100M)
<bean> <property name="messageConverters"> <list> <!--Configure download return type--><bean/> <bean> <!--Configure encoding method--><property name="supportedMediaTypes" value="application/json; charset=UTF-8" /> </bean> </list> </property> </bean>
Download file code
@RequestMapping("/file/{name.rp}")public ResponseEntity<byte[]> fileDownLoad(@PathVariable("name.rp")String name, HttpServletRequest request,HttpServletResponse response) {// @PathVariable String name,// @RequestParam("name")String name,// System.out.println("<name>"+name);// System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");ResponseEntity<byte[]> re = null;try {/*** css,js,json,gif,png,bmp,jpg,ico,doc,doc,docx,xlsx,txt,swf,pdf* **///Download to prevent static loading interference Feelutile f=new Feelutile();name=f.getfileformat(name);String pathString="C://tempDirectory//"+name; File file=new File(pathString);HttpHeaders headers=new HttpHeaders();//String filename=URLEncoder.encode(name, "UTF-8");//To solve the problem of garbled Chinese name String filename=new String(name.getBytes("utf-8"),"utf-8");byte[] by=FileUtils.readFileToByteArray(file);headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);//URLEncoder.encode(filename, "UTF-8")headers.setContentDispositionFormData("attachment",filename);headers.setContentLength(by.length);re=new ResponseEntity<byte[]>(by, headers, HttpStatus.CREATED);} catch (Exception e) {e.printStackTrace();try {request.getRequestDispatcher("/error/404.jsp").forward(request, response);} catch (ServletException e1) {// TODO Auto-generated catch blocke1.printStackTrace();} catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}}return re;}Upload file:
1Configure in spring-mvc
<!--4. File upload configuration file upload --><bean id="multipartResolver"class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><property name="defaultEncoding"><value>UTF-8</value></property><property name="maxUploadSize"><value>1048576000</value></property><property name="maxInMemorySize"><value>40960</value></property></bean>
The code in the controller is as follows
@RequestMapping(value="/upload", method = RequestMethod.POST)@ResponseBodypublic Json upload(Doc doc, @RequestParam("uploadFile") CommonsMultipartFile file) {Json j = new Json();try {String realpath = this.servletContext.getRealPath("/upload"); String uploadFileFileName = file.getOriginalFilename(); String uploadFileFileNameWithoutSpace = uploadFileFileName.replaceAll(" ", ""); String fileType = uploadFileFileNameWithoutSpace.substring(uploadFileFileNameWithoutSpace.lastIndexOf("."));File targetFile = new File(realpath+File.separator, uploadFileFileNameWithoutSpace); if (targetFile.exists()) {targetFile.delete();}file.getFileItem().write(targetFile); docService.upload(doc,uploadFileFileNameWithoutSpace);j.setSuccess(true);j.setMsg("Upload manual successfully");}catch (Exception e) {logger.error(ExceptionUtil.getExceptionMessage(e));j.setMsg("Upload manual unsuccessfully");}return j;}The above is a detailed explanation of the Java Spring MVC upload and download file configuration and controller method introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!