spring mvc 附件上传至腾讯云qcloud

阅读更多

简单记录主要是便于自己用,有需要的参考一下...

 

上传至腾讯云,相关文档参阅官方文档

 

附件为比较早的版本,自己修改过

 

1、实体bean --用multipartFile接收

public class UploadForm {

        //上传的图片

private MultipartFile PhotoFile;

 

        public MultipartFile getPhotoFile() {

return PhotoFile;

}

public void setPhotoFile(MultipartFile photoFile) {

PhotoFile = photoFile;

}

}

 

2、controller

    @RequestMapping(value="/uploadImage")

@ResponseBody

public String uploadImage(UploadForm uploadForm,Model model ){

return uploadService.uploadImage(uploadForm,model);

}

 

3、service

@Transactional

public String uploadImage(UploadForm uploadForm,Model model ){

 

//图片类型

String imageType = siteForm.getImageType();

 

MultipartFile uploadFile = siteForm.getPhotoFile();

 

UploadResult result = PicUploadUtil.picUploadByFileAstrict(uploadFile, 1920, 572, 0, new String[]{"jpg","jpeg","png"}, false);

if(result.result != 0)

return "2";

//将图片信息保存到数据库

CtAttachment attachmentExample = new CtAttachment();

attachmentExample.setUserId(SecurityUserHelper.getCurrentUser().getPersonId());

attachmentExample.setAttCode(imageType);

attachmentExample.setAttName(uploadFile.getOriginalFilename());

attachmentExample.setAttUrl(result.download_url);

this.ctAttachmentDAO.insertNotNull(attachmentExample);

 

return result.download_url;

}

 

4、PicUploadUtil

/**

* 图片上传

* @param file

* @param width 宽度限制 <=0表示不校验

* @param height 高度限制 <=0表示不校验

* @param size 大小限制 <=0表示不校验

* @param suffix 后缀限制 null或length<0表示不校验

* @param needCompress 是否需要压缩

* @return

*/

public static UploadResult picUploadByFileAstrict(MultipartFile file,int width,int height,long size,String [] suffix,boolean needCompress) {

 

UploadResult result = new UploadResult();

 

try {

String imgName = file.getOriginalFilename();

String imgSuffix = imgName.substring(imgName.lastIndexOf(".") + 1);

 

//不用添加后缀

String imgNameTemp = UUID.randomUUID().toString();

File fileTemp = new File(picTempDir + imgNameTemp);

 

//这里只使用路径,而不创建真实文件,所有也不用考虑删除文件的问题

if(fileTemp.exists()){

log.error("target file is exists , create fail !");

result.result = -1;

return result;

}

 

byte[] advImageBytes = null;  

   InputStream advImageStream = null;

   

   advImageStream = file.getInputStream();  

       advImageBytes = FileCopyUtils.copyToByteArray(advImageStream);  

       FileCopyUtils.copy(advImageBytes, fileTemp);  

       advImageStream.close();  

       BufferedImage buff = ImageIO.read(fileTemp);

       

       if((width > 0 ? buff.getWidth() > width : false) || (height > 0 ? buff.getHeight() > height : false) || 

        (size > 0 ?file.getSize() > size : false) || ((null != suffix&&suffix.length>0)?!ArrayUtils.contains(suffix, imgSuffix):false)){

        //TODO 如果需要压缩,则直接压缩到指定大小,不用校验

        /*//返回之前先删除添加的文件,

        fileTemp.delete();*/

        result.result = 1;

        return result;

       }

       

       PicCloud pc = new PicCloud(APP_ID, SECRET_ID, SECRET_KEY);

 

int ret = pc.UploadByFile("", fileTemp, result);

 

if(ret == 0){

result.result = 0;

}else{

result.result = -1;

}

return result;

 

} catch (Exception e) {

log.error(e);

}

 

return null;

 

}

 

5、jsp页面

 

6、spring-mvc 配置

 

         

             

             

         

         

         

       

         

           

                 

                   

                   error_fileupload  

                 

           

       

  • qcloud.zip (7.7 KB)
  • 下载次数: 1

你可能感兴趣的:(java,MultipartFile,qcloud)