jsp:代码
<input type="file" name="applyFile" id="applyFile" capture="camera" accept="application/pdf,image/*" multiple="multiple" >
<img alt="" src="" id = "testImg" width="140" height="180">
var filedata = new Array();
$("#applyFile").change(function(){
debugger
var file = document.getElementById("applyFile").files[0];
if(file) {
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function(){
var imgstr = this.result; //这就是base64字符串
var image = document.getElementById("testImg");
image.src = imgstr;
filedata[0] = imgstr;
alert(imgstr);
var imageName = '111.pdf';
//这里可换成ajax请求因为本框架不是springmvc没有使用ajax
PlatformContext.getJSONBean('registerJsBean',appId).uploadImage(imageName,imgstr)
.done(function(returnProduct){
alert("成功");
}).fail(function(err){
alert(err);
});
};
}
});
java代码:
public TransResult
Map
imageBase64=imageBase64.substring(imageBase64.indexOf(",")+1);
imgMap.put(imageName, imageBase64);
VisitService visitService = (VisitService) ApplicationContextUtil.getBean("visitServiceImpl");
TransResult
return null;
}
@Override
public TransResult
// TODO Auto-generated method stub
TransResult
OutputStream out = null;
String imgTempPath = PropertiesUtil.getProperties("ftp.properties").getProperty(VisitConstant.VISIT_IMAGE_TEMP_PATH);
try {
for(String imgName : imgMap.keySet()){
BASE64Decoder decoder = new BASE64Decoder();
String imageBase64=imgMap.get(imgName);
imageBase64=imageBase64.substring(imageBase64.indexOf(",")+1);
byte[] bytes = decoder.decodeBuffer(imageBase64);
for (int i = 0; i < bytes.length; ++i) {
if (bytes[i] < 0) {// 调整异常数据
bytes[i] += 256;
}
}
String path = imgTempPath+ imgName;
// 生成jpeg图片
out = new FileOutputStream(path);
out.write(bytes);
out.flush();
}
result.success();
result.setObject(imgTempPath);
} catch (Exception e) {
result.failure("保存图片过程中异常");
Logger.info("[回访服务][保存图片][异常]");
Logger.printStackTrace(e);
}finally {
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return result;
}