解析图片流

来源:互联网 发布:办公楼pert网络布线图 编辑:程序博客网 时间:2024/04/27 21:25

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;

}



0 0
原创粉丝点击