spring mvc 上传下载文件demo(上传到mysql里面)

spring mvc 上传文件代码:

 @RequestMapping(value = "/attach/upload")
	  @ControllerLogAnnotation(moduleName="服务管理-合同管理-附件",option="上传") 
	    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("上传成功!").json();
	            
	            
	        } catch (Exception e) {
//	        	redirectAttributes.addFlashAttribute("message", "流程部署失败!");
	        	jsonObject = JsonResultBuilder.success(false).msg("上传失败!").json();
	            logger.error("上传失败!", e);
	        }

	        
	        writeJson(response,jsonObject); 
	        
	    }

 数据mysql,直接写大数据到数据库的。

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已经取消createBlob
	
	private byte[] attachContent;
	private String attachName;
..
@Column(name = "contract_id")
	public String getContractId() {
		return contractId;
	}

	@Column(name = "attach")
	public byte[] getAttachContent() {
		return attachContent;
	}
}

 表
spring mvc 上传下载文件demo(上传到mysql里面)_第1张图片
 此时可以上传任意文件,zip,rar,txt,jpg等等。但是如果上传文件过大会报错:mysql大数据默认只能最大1M数据,需要修改max_allowed_packet ,怎么改,看MySQL max_allowed_packet设置及问题。 同时应该也设置spring mvc里面的参数

	  
	  
		  
	

 

-------------------------------

 

下载没什么好说的,看代码:

前台弄个href连接到后台就行

  @RequestMapping(value = "/attach/exportFile/{attachId}")
		@ControllerLogAnnotation(moduleName="服务管理-合同管理-附件",option="下载")
	  public ResponseEntity 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(attachContent,    
	                                          headers, HttpStatus.CREATED);    
		  
	  }

 下载参考:http://blog.csdn.net/clj198606061111/article/details/20743769

你可能感兴趣的:(Spring-mvc,mysql)