Java-spring-mvc _ configure the controller file in the upper and lower layers, java-spring-mvc
Download:
1. Configure in spring-mvc (for downloading files below MB)
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
- <! -- Configure the download return type -->
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
- <! -- Configure the 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, docx, xls, 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 Chinese name garbled Problem
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 block
E1.printStackTrace ();
} Catch (IOException e1 ){
// TODO Auto-generated catch block
E1.printStackTrace ();
}
}
Return re;
}
Upload File: 1 configure 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 controller is as follows:
@RequestMapping(value="/upload", method = RequestMethod.POST)
@ResponseBody
public 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;
}