ios端用的是base64 然后 我用的springmvc接收上传的转码为base64的图片
但是总不成功 西面是代码:
我用了一个公共类:
public static boolean generateImage(String imgStr,String imgFile)throws Exception{
//对字节数组字符串进行Base64解码并生成图片
if(imgStr==null){return false;}//图像数据为空
BASE64Decoder decoder=new BASE64Decoder();
try {
//base64解码
byte[] b=decoder.decodeBuffer(imgStr);
for (int i = 0; i < b.length; i++) {
if(b[i]<0){
b[i]+=256;//调整异常数据
}
}
//生成jpeg图片
String imgFilePath=imgFile;//新生成的图片
OutputStream out=new FileOutputStream(imgFilePath);
out.write(b);
out.flush();
out.close();
return true;
} catch (Exception e) {
throw e;
}
}
然后再controller层:
@ResponseBody
@RequestMapping(value="/uploadfile",method=RequestMethod.GET)
public String handleFileUpdate(@RequestParam("file") String file){
System.out.println(file);
String imgFile="D:/apache-tomcat-7.0.50/apache-tomcat-7.0.50/webapps/xiangmu/images/test1.jpeg";
long startTime=System.currentTimeMillis();//获取开始时间
try {
if(YanZheng.generateImage(file, imgFile)==true){
long endTime=System.currentTimeMillis();//获取结束时间
System.out.println("上传文件共使用时间:"+(endTime-startTime));
return "success";
}else{return "fail";}
} catch (Exception e) {
e.printStackTrace();
return "fail";
}
}
是代码有错吗?还是思路不对?那有没有别的方法接收ios端的图片呢?