spring mvc upload file code:
@RequestMapping(value = "/attach/upload") @ControllerLogAnnotation(moduleName="Service Management-Contract Management-Attachment",option="Upload") public void upload( String contractId, @RequestParam(value = "file", required = false) MultipartFile file, RedirectAttributes redirectAttributes, HttpServletResponse response) { String fileName = file.getOriginalFilename(); JSONObject jsonObject= new JSONObject(); try { InputStream fileInputStream = file.getInputStream(); String extension = FilenameUtils.getExtension(fileName); ContractAttach attach = new ContractAttach(); attach.setAttachName(fileName); attach.setContractId(contractId); byte[] attachContent =FileCopyUtils.copyToByteArray(fileInputStream); attach.setAttachContent(attachContent); service.addObj(attach); jsonObject = JsonResultBuilder.success(true).msg("Upload successful!").json(); } catch (Exception e) { // redirectAttributes.addFlashAttribute("message", "Process deployment failed!"); jsonObject = JsonResultBuilder.success(false).msg("Upload failed!").json(); logger.error("Upload failed!", e); } writeJson(response,jsonObject); }
Data mysql, write big data directly to the database.
entity:
@Entity @Table(name = "t_contract_attach") public class ContractAttach implements java.io.Serializable { // Fields private String id; private String contractId; // private String attach; // @Lob // private Blob attachContent; //hibernate4 has canceled createBlob private byte[] attachContent; private String attachName; .. @Column(name = "contract_id") public String getContractId() { return contractId; } @Column(name = "attach") public byte[] getAttachContent() { return attachContent; } }
The table
can upload any file at this time, zip, rar, txt, jpg, etc. However, if the upload file is too large, an error is reported: MySQL big data can only have a maximum of 1M data by default, and you need to modify max_allowed_packet . How to change it, see MySQL max_allowed_packet settings and problems . At the same time, the parameters in spring mvc should also be set
<!-- Upload file interception, set the maximum upload file size 10M=10*1024*1024(B)=10485760 bytes --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="${web.maxUploadSize}" /> </bean>
-------------------------------
There is nothing to say about downloading, look at the code:
Just get a href in the foreground to connect to the background
@RequestMapping(value = "/attach/exportFile/{attachId}") @ControllerLogAnnotation(moduleName="Service Management-Contract Management-Attachment",option="download") public ResponseEntity<byte[]> exportFile( @PathVariable("attachId")String attachId, HttpServletResponse response) throws IOException { ContractAttach attach = service.uniqueEntity(ContractAttach.class, "id", attachId); String fileName = attach.getAttachName(); byte[] attachContent = attach.getAttachContent(); HttpHeaders headers = new HttpHeaders(); headers.setContentDispositionFormData("attachment", fileName); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); return new ResponseEntity<byte[]>(attachContent, headers, HttpStatus.CREATED); }
Download reference: http://blog.csdn.net/clj198606061111/article/details/20743769