图片上传方法

来源:互联网 发布:java第一阶段测试题 编辑:程序博客网 时间:2024/05/08 10:59
@RequestMapping(value = "/uploadImage", method = RequestMethod.POST)
@ResponseBody
public String uploadImage(String imgdata, HttpServletRequest request){
BASE64Decoder decoder = new BASE64Decoder();  
        FileOutputStream outputStream =null;
     //   String uploadSavePath = "D:\\spread-bcn\\upload\\image\\";
        String uploadSavePath = PropertiesConfig.getImagePath();
        
        String returnString = "";
        try {
      SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
      SimpleDateFormat df=new SimpleDateFormat("yyyyMMddhhmmss");
      String today=sdf.format(new Date());
           //String savePath =  PropertiesConfig.getImagePath()+today;
           
           //检查扩展名
           String newFileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + ".png";
           String savePath =  uploadSavePath+today;
           File dirFile = new File(savePath);
  if (!dirFile.exists()) {
  dirFile.mkdirs();
  }
  savePath = TxtUtil.converSeparator(savePath);
           // new一个文件对象用来保存图片
           File imageFile = new File(savePath,newFileName); 
           // 创建输出流  
           outputStream = new FileOutputStream(imageFile);  
           // 获得一个图片文件流
           byte[] result = decoder.decodeBuffer(imgdata.split(",")[1]);//解码  
for (int i = 0; i < result.length; ++i) {
if (result[i] < 0) {// 调整异常数据
result[i] += 256;
}
}
           outputStream.write(result); 
           returnString= uploadSavePath+today+File.separator+newFileName;
//           returnString= today+File.separator+newFileName;
} catch (IOException e) {
return "error";
} finally {
try {
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
       return returnString;

}


function onChangeUploadImage(that){  
   var filepath=$(that).val();  
   if(filepath=="")  
   {  
       return;  
   }  
   var extStart=filepath.lastIndexOf(".");  
   var ext=filepath.substring(extStart,filepath.length).toUpperCase();  
   if(".jpg|.png|.jpeg".toUpperCase().indexOf(ext.toUpperCase())==-1){  
      alert("只允许上传jpg、png、jpeg格式的图片");  
       return false;
   }  
   //以图片宽度为800进行压缩  
   lrz(that.files[0], {  
    width: 800,
    quality: 0.8
   }).then(function (rst) {  
       $.ajax({  
      type: "POST",
       url : "${ctx}"+"/countrySubInfo/uploadImage",
       data : {  
           imgdata:rst.base64//压缩后的base值  
       },  
       dataType:"text",  
       success : function(data) {
        if ("error"==data) {
        alert("上传文件失败!");
        }else{
        alert(data);
        $("#filepathsave").val(data);
        //uploadFile(data);
        }
       },  
    error : function(){  
           alert("上传失败");  
       }  
  });  
});  
}


<input type="file" class="mp_file" name="file" id="files"
onchange="onChangeUploadImage(this)" multiple="multiple"
style="opacity: 1"> <img src="${ctx}/${subject.picUrl}"
id="imgs"> 
<%-- <input name="imageUrl" id="imageUrl" type="hidden" value="${subject.picUrl}" /> --%>
<input name="filepathsave" id="filepathsave" type="hidden" value="${subject.picUrl}"/>
<!--<span>请上传图片</span>-->

原创粉丝点击