Spring mvc JS上传文件

在Spring mvc xml 中添加

 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 指定所上传文件的总大小不能超过800KB......注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 -->
        <property name="maxUploadSize" value="500000"/>
    </bean>
    
     <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">    
       <property name="exceptionMappings">    
           <props>    
               <!-- 遇到MaxUploadSizeExceededException异常时,跳转到/page/html/errorGolbal.html页面 -->    
               <prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">/page/errorGolbal</prop>    
           </props>    
       </property>    
   </bean>  

上传图片代码

@ResponseBody
 	@RequestMapping("/fileUpload")
    public String fileUpload( @RequestParam MultipartFile[] myfiles, HttpServletRequest request, HttpServletResponse response) throws Exception{
        String realPath = request.getSession().getServletContext().getRealPath("/upload/article");
        response.setContentType("text/plain; charset=UTF-8");
        PrintWriter out = response.getWriter();
        long fileSize = 0;
        int height = 0;
        int width = 0;
        String fileName = null;
        String path = null;
        for(MultipartFile myfile : myfiles){
            if(myfile.isEmpty()){
                out.print("{\"message\":\"请选择图片再次上传!!\",\"status\":-1}");
                out.flush();
                return null;
            }else{
                try {
        		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
                	fileSize = myfile.getSize(); //图片大小
	            	String str = myfile.getOriginalFilename();
	            	String suffix = str.substring(str.indexOf("."));
	            	String name = str.substring(0, str.indexOf("."));
	            	fileName = MD5Util.md5Encode(df.format(new Date()))+suffix; //名称
                	FileUtils.copyInputStreamToFile(myfile.getInputStream(), new File(realPath, fileName));
                } catch (Exception e) {
                    out.print("{\"message\":\"上传失败\",\"status\":-1}");
                    out.flush();
                    return null;
                }
            }
        }
        
        path = request.getContextPath() + "/upload/article/" + fileName; //url路径
        String filepath = request.getContextPath() + "/upload/article/";
    	BufferedImage bufferedImage = ImageIO.read(new File(realPath+'/'+fileName));
    	width = bufferedImage.getWidth(); //宽度
    	height = bufferedImage.getHeight(); //高度
        
        out.print("{\"data\":{\"path\":\""+path+"\",\"size\":\""+fileSize+"\",\"width\":\""+width+"\",\"height\":\""+height+"\",\"fileName\":\""+fileName+"\",\"filepath\":\""+filepath+"\"},\"status\":1}");
        out.flush();
        return null;
    }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值