|

楼主 |
发表于 2017-12-2 02:21:16
|
显示全部楼层
java代码如下
- package nkyz;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
- import org.apache.commons.lang.StringUtils;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.justep.baas.action.ActionContext;
- import common.DBHelper;
- public class IncidentService {
-
- //上传文件根路径,注意是本地文件路径,一般定位到tomcat的webapps文件夹。需要配置[数据库中配置会覆盖此处默认配置]
- public static String UPLOAD_ROOT_DIR = "J:\\webapps\";//注意以\\结尾
- public static String UPLOAD_FOLDER = "uploads";//本地文件夹名:上传文件存放的文件夹名
- public static String UPLOAD_SERVER = "";//上传服务器地址:IP及端口,前台展示图片会用到
- /**
- * 保存事件处理
- */
- public static JSONObject saveIncidentDeal(JSONObject params, ActionContext context) throws Exception {
- JSONObject result = new JSONObject();
- result.put("ok", false);
- Object id = params.get("id");
- if(id == null){
- result.put("errorMsg", "事件ID不能为空!");
- return result;
- }
- //上传图片
- String imageUrls = null;
- JSONArray imageUrlsArr = params.getJSONArray("imageUrlsArr");
- if(imageUrlsArr!=null && imageUrlsArr.size()>0){
- List<String> imageSrcArr = new ArrayList<String>();
- for(int i=0; i<imageUrlsArr.size(); i++){
- JSONObject img = imageUrlsArr.getJSONObject(i);
- JSONObject rtn = uploadImageAjax(img, context);
- if(!rtn.getBooleanValue("ok")){
- return rtn;
- }else{
- String imageSrc = rtn.getString("imageSrc");
- imageSrcArr.add(imageSrc);
- }
- }
- imageUrls = StringUtils.join(imageSrcArr, ",");
- }
- //插入事件处理
- String sql = "INSERT INTO incident_deal (incidentId, description, imageUrls, createTime, creator) " +
- " VALUES (?, ?, ?, now(), ?);";
- List<Object> sqlParams = new ArrayList<Object>();
- sqlParams.add(id);
- sqlParams.add(params.get("description"));
- sqlParams.add(imageUrls);
- sqlParams.add(params.get("username"));
- DBHelper.execUpdate(sql, sqlParams.toArray());
- //更改事件状态为已处理
- String sql2 = "update incident set state = 4, chuliTime = now() where id = ? ";
- DBHelper.execUpdate(sql2, id);
-
- result.put("ok", true);
- return result;
- }
-
-
- /**
- * 裁剪后通过AJAX上传图片
- */
- public static JSONObject uploadImageAjax(JSONObject params, ActionContext context) throws SQLException, NamingException {
- JSONObject result = new JSONObject();
- String imageData = params.getString("imageData"); // 拿到字符串格式的图片
- String imageName = params.getString("imageName");
- String ownerID = params.getString("ownerID");
- String docStorePath = UPLOAD_ROOT_DIR + UPLOAD_FOLDER + File.separator + ownerID + File.separator;
- String imgFilePath = docStorePath + imageName; // 指定图片要存放的位置
- imageData = imageData.split(",")[1];// 去掉头部
- BASE64Decoder decoder = new BASE64Decoder();
- try {
- File imageFile = new File(imgFilePath);
- //判断目标文件所在的目录是否存在
- if(!imageFile.getParentFile().exists()) {
- imageFile.getParentFile().mkdirs();
- }
- byte[] decodedBytes = decoder.decodeBuffer(imageData); // 将字符串格式的image转为二进制流(biye[])的decodedBytes
- FileOutputStream out = new FileOutputStream(imgFilePath); // 新建一个文件输出器,并为它指定输出位置imgFilePath
- out.write(decodedBytes); // 利用文件输出器将二进制格式decodedBytes输出
- out.close(); // 关闭文件输出器
- String imageSrc = "/" + UPLOAD_FOLDER + "/" + ownerID + "/" + imageName;//该路径保存在数据库中,前台展示用
- result.put("ok", true);
- result.put("imageSrc", imageSrc);
- return result;
- } catch (Exception e) {
- e.printStackTrace();
- result.put("ok", false);
- result.put("errorMsg", "上传文件失败!" + e.getMessage());
- return result;
- }
-
- }
- }
复制代码
|
|