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页面

<div id="updateBox" style="display:none;">

<input style="border:0;width:190px;" type="file" id="PhotoFile" name="PhotoFile"/>

<button class="leeer" type="button" onclick="addPhoto(<c:out value='${model.id }'/>);">上传图片</button>              

              <!--<button class="leeer" onclick="updateImg(1);return false;">关闭</button>

 --></div>

6、spring-mvc 配置

<!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 -->  

        <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  

           <!-- 指定所上传文件的总大小不能超过200KB。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 -->  

           <property name="maxUploadSize" value="10000000"/>  

       </bean>  

         

       <!-- SpringMVC在超出上传文件限制时,会抛出org.springframework.web.multipart.MaxUploadSizeExceededException -->  

       <!-- 该异常是SpringMVC在检查上传的文件信息时抛出来的,而且此时还没有进入到Controller方法中 -->

       <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">  

         <property name="exceptionMappings">  

               <props>  

                  <!-- 遇到MaxUploadSizeExceededException异常时,自动跳转到error_fileupload.jsp页面 -->  

                   <prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">error_fileupload</prop>  

               </props>  

          </property>  

       </bean>

猜你喜欢

转载自godsend-jin.iteye.com/blog/2268497