解析图片流

private AppFeedbackService appFeedbackService ;
public static final String UPLOAD="upload/";
public void setAppFeedbackService(AppFeedbackService appFeedbackService) {
this.appFeedbackService = appFeedbackService;
}

public String appFeedbackAdd() throws Exception{

Map<String, String> jsonMap = new HashMap<String, String>();
String params = request().getParameter("data");
String userId = CJSON.getUserId(params);
String feedback = CJSON.getData(params,"feedback");
String img = CJSON.getData(params,"file"); //图片
String fileName = CJSON.getData(params,"fileName"); //文件名
try {
//判断文件为空
   if(img==null||img==null){
    jsonMap.put("code", "1");
jsonMap.put("msg", "文件为空");
CJSON.printObject(false, jsonMap);
return null;
   }
 //判断文件类型
   String fileType = fileName.substring(fileName.lastIndexOf("."),fileName.length());
   if(!".jpg".equals(fileType.toLowerCase())&&!".png".equals(fileType.toLowerCase())&&!".jpeg".equals(fileType.toLowerCase())){
    jsonMap.put("code", "2");
jsonMap.put("msg", "文件格式错误");
CJSON.printObject(false, jsonMap);
return null;
   }
   String dateStr = new SimpleDateFormat("yyyyMMdd").format(new Date()); 
String realPath = ServletUtils.serverRootDirectory()+ UPLOAD + "headImg"+"/"+ dateStr+ "/";
FileUtils.mkdirs(realPath);
if(!UploadUtil.GenerateImage(img, realPath+fileName)){
jsonMap.put("code", "3");
jsonMap.put("msg", "图片上传失败");
CJSON.printObject(false, jsonMap);
return null;
}
//截取文件路径更新数据
   String filePath = realPath.substring(realPath.indexOf("upload"),realPath.length())+fileName;
  long id = appFeedbackService.addFeedbackApp(userId,feedback,filePath);
if(id>0){
jsonMap.put("code", "-1");
jsonMap.put("msg", "反馈成功");
CJSON.printObject(true, jsonMap);
return null;
}else{
jsonMap.put("code", "1");
jsonMap.put("msg", "反馈失败");
CJSON.printObject(false, jsonMap);
return null ;
}
} catch (Exception e) {
e.printStackTrace();
jsonMap.put("code", "2");
jsonMap.put("msg", "未知错误");
CJSON.printObject(false, jsonMap);
}
return null;

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值