• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

mysql 搞代码 7年前 (2018-06-09) 113次浏览 已收录 0个评论

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里面)_mysql
 此时可以上传任意文件,zip,rar,txt,jpg等等。但是如果上传文件过大会报错:mysql大数据默认只能最大1M数据,需要修改max_allowed_packet ,怎么改,看MySQL max_allowed_packet设置及问题。 同时应该也设置spring mvc里面的参数

 <!-- 上传文件拦截,设置最大上传文件大小   10M=10*1024*1024(B)=10485760 bytes -->    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">     <property name="maxUploadSize" value="${web.maxUploadSize}" />    </bean>

 

——————————-

 

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

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

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

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

欢迎大家阅读《spring mvc 上传下载文件demo(上传入mysql里面)_mysql》,跪求各位点评,by 搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:spring mvc 上传下载文件demo(上传入mysql里面)_mysql

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址