203 lines
7.0 KiB
Plaintext
203 lines
7.0 KiB
Plaintext
|
|
package com.nationalelectric.greenH5;
|
|||
|
|
|
|||
|
|
import java.io.ByteArrayInputStream;
|
|||
|
|
import java.io.IOException;
|
|||
|
|
import java.io.InputStream;
|
|||
|
|
import java.net.URLDecoder;
|
|||
|
|
import java.util.ArrayList;
|
|||
|
|
import java.util.Date;
|
|||
|
|
import java.util.Enumeration;
|
|||
|
|
import java.util.List;
|
|||
|
|
import java.util.UUID;
|
|||
|
|
|
|||
|
|
import javax.annotation.Resource;
|
|||
|
|
import javax.servlet.http.HttpServletRequest;
|
|||
|
|
import javax.servlet.http.HttpServletResponse;
|
|||
|
|
|
|||
|
|
import org.hibernate.Hibernate;
|
|||
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|||
|
|
import org.springframework.stereotype.Controller;
|
|||
|
|
import org.springframework.transaction.annotation.Transactional;
|
|||
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|||
|
|
import org.springframework.web.bind.annotation.RequestMethod;
|
|||
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|||
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|||
|
|
import org.springframework.web.multipart.MultipartFile;
|
|||
|
|
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
|||
|
|
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
|
|||
|
|
|
|||
|
|
import com.nationalelectirc.Constant.Constant;
|
|||
|
|
import com.nationalelectirc.utils.RestResult;
|
|||
|
|
import com.nationalelectirc.utils.Utils;
|
|||
|
|
import com.nationalelectric.greenH5.bizc.BaseServiceImpl;
|
|||
|
|
import com.nationalelectric.greenH5.bizc.IGreenImageInfoBizc;
|
|||
|
|
import com.nationalelectric.greenH5.po.GreenImageInfo;
|
|||
|
|
import com.sun.xml.internal.messaging.saaj.util.ByteInputStream;
|
|||
|
|
|
|||
|
|
import Decoder.BASE64Encoder;
|
|||
|
|
import net.sf.json.JSONObject;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* <b>概述</b>:<br>
|
|||
|
|
* <p>
|
|||
|
|
* <b>功能</b>:<br>
|
|||
|
|
*
|
|||
|
|
* @author dell
|
|||
|
|
*/
|
|||
|
|
@Controller
|
|||
|
|
@RequestMapping("/greenImageInfo")
|
|||
|
|
public class GreenImageInfoController {
|
|||
|
|
/**
|
|||
|
|
* greenimageinfo GreenImageInfo逻辑构件
|
|||
|
|
*/
|
|||
|
|
@Resource
|
|||
|
|
private IGreenImageInfoBizc greenimageinfoBizc;
|
|||
|
|
|
|||
|
|
@Resource
|
|||
|
|
private GreenImageInfoController greenImageInfoController;
|
|||
|
|
|
|||
|
|
@Autowired
|
|||
|
|
private BaseServiceImpl baseService;
|
|||
|
|
|
|||
|
|
@SuppressWarnings("static-access")
|
|||
|
|
@RequestMapping("/uploadImgBuGroup")
|
|||
|
|
public @ResponseBody RestResult uploadImgBuGroup(HttpServletRequest request, HttpServletResponse response) {
|
|||
|
|
try {
|
|||
|
|
byte buffer[];
|
|||
|
|
buffer = getRequestPostBytes(request);
|
|||
|
|
String jsonStr = new String(buffer, "UTF-8");
|
|||
|
|
JSONObject jsonObject = JSONObject.fromObject(jsonStr.trim());
|
|||
|
|
String base64 = jsonObject.getString("base64");
|
|||
|
|
String userId = jsonObject.getString("userId");
|
|||
|
|
String groupEnd = jsonObject.getString("groupEnd");
|
|||
|
|
if("0".equals(groupEnd)){
|
|||
|
|
String mapBase64 = Utils.map4Img.get(userId)==null?"":Utils.map4Img.get(userId);
|
|||
|
|
mapBase64 = mapBase64+base64;
|
|||
|
|
Utils.map4Img.put(userId, mapBase64);
|
|||
|
|
return new RestResult(Constant.SUCCESS, "成功");
|
|||
|
|
}else {
|
|||
|
|
String mapBase64 = Utils.map4Img.get(userId)==null?"":Utils.map4Img.get(userId);
|
|||
|
|
mapBase64 = mapBase64+base64;
|
|||
|
|
String uuid = "";
|
|||
|
|
uuid = greenImageInfoController.saveImage(mapBase64, userId.substring(0, userId.length()-1));
|
|||
|
|
baseService.saveImgToIO(uuid+Constant.IMG_SUFFIX, mapBase64);
|
|||
|
|
Utils.map4Img.put(userId, "");
|
|||
|
|
return new RestResult(Constant.SUCCESS, "成功",uuid);
|
|||
|
|
}
|
|||
|
|
}catch(Exception e){
|
|||
|
|
e.printStackTrace();
|
|||
|
|
return new RestResult(Constant.FAILED, "失败");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
@SuppressWarnings("deprecation")
|
|||
|
|
@RequestMapping(value="/uploadImg", method = RequestMethod.POST)
|
|||
|
|
@ResponseBody
|
|||
|
|
public RestResult uploadOssFile(HttpServletRequest request) {
|
|||
|
|
try {
|
|||
|
|
System.out.println(11111);
|
|||
|
|
CommonsMultipartResolver cmr = new CommonsMultipartResolver(request.getServletContext());
|
|||
|
|
cmr.setDefaultEncoding("utf-8");
|
|||
|
|
cmr.setMaxInMemorySize(409600);
|
|||
|
|
cmr.setMaxUploadSize(10485760000L);
|
|||
|
|
MultipartHttpServletRequest multipartRequest = cmr.resolveMultipart(request);
|
|||
|
|
|
|||
|
|
MultipartFile file = multipartRequest.getFile("file");
|
|||
|
|
System.out.println(222);
|
|||
|
|
List<String> list = new ArrayList<String>();
|
|||
|
|
System.out.println(333);
|
|||
|
|
|
|||
|
|
// for(int i=0;i<files1.length;i++){
|
|||
|
|
// MultipartFile file = files1[i];
|
|||
|
|
if (file == null || file.isEmpty()) {
|
|||
|
|
throw new RuntimeException("图片不能为空!");
|
|||
|
|
}
|
|||
|
|
String fileName = file.getOriginalFilename();
|
|||
|
|
String fileType = fileName.substring(fileName.lastIndexOf("."));
|
|||
|
|
String contentType = file.getContentType();
|
|||
|
|
System.out.println(2222);
|
|||
|
|
byte[] imageBytes = new byte[]{};
|
|||
|
|
System.out.println(3333);
|
|||
|
|
String base64EncoderImg="";
|
|||
|
|
imageBytes = file.getBytes();
|
|||
|
|
System.out.println(4444);
|
|||
|
|
BASE64Encoder base64Encoder =new BASE64Encoder();
|
|||
|
|
base64EncoderImg = "data:" + contentType + ";base64," + base64Encoder.encode(imageBytes);
|
|||
|
|
base64EncoderImg = base64EncoderImg.replaceAll("[\\s*\t\n\r]", "");
|
|||
|
|
System.out.println(5555);
|
|||
|
|
String uuid = saveImage(base64EncoderImg,"admin");
|
|||
|
|
baseService.saveImgToIO(uuid+Constant.IMG_SUFFIX,base64EncoderImg );
|
|||
|
|
list.add(uuid+Constant.IMG_SUFFIX);
|
|||
|
|
// }
|
|||
|
|
return new RestResult(Constant.SUCCESS,"上传图片成功",list);
|
|||
|
|
} catch (Exception e) {
|
|||
|
|
// TODO Auto-generated catch block
|
|||
|
|
e.printStackTrace();
|
|||
|
|
return new RestResult(Constant.FAILED, "上传图片失败");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 保存图片
|
|||
|
|
*
|
|||
|
|
* @param image
|
|||
|
|
* @param userId
|
|||
|
|
* @return
|
|||
|
|
* @throws Exception
|
|||
|
|
*/
|
|||
|
|
public String saveImage(String image, String userId) throws Exception {
|
|||
|
|
// image = URLDecoder.decode(image, "UTF-8");
|
|||
|
|
// 生成uuid
|
|||
|
|
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
|
|||
|
|
GreenImageInfo greenImageInfo = new GreenImageInfo();
|
|||
|
|
// 主键id
|
|||
|
|
greenImageInfo.setId(uuid);
|
|||
|
|
// 图片
|
|||
|
|
greenImageInfo.setImageContent("---");
|
|||
|
|
//处理BLOB图片
|
|||
|
|
InputStream is = new ByteArrayInputStream(image.getBytes());
|
|||
|
|
byte[] byteArray = new byte[is.available()];
|
|||
|
|
is.read(byteArray);
|
|||
|
|
is.close();
|
|||
|
|
greenImageInfo.setImageContentBlob(Hibernate.createBlob(byteArray));
|
|||
|
|
// greenImageInfo.setImageContentClob(Hibernate.createClob(image));
|
|||
|
|
// 创建者
|
|||
|
|
greenImageInfo.setCreator(userId);
|
|||
|
|
// 修改者
|
|||
|
|
greenImageInfo.setModifier(userId);
|
|||
|
|
// 创建时间
|
|||
|
|
greenImageInfo.setGmtCreated(new Date());
|
|||
|
|
// 修改时间
|
|||
|
|
greenImageInfo.setGmtModified(new Date());
|
|||
|
|
// 删除标识
|
|||
|
|
greenImageInfo.setIsDeleted("N");
|
|||
|
|
// 图片状态1web上传 2app上传 3web已同步 4app已同步
|
|||
|
|
greenImageInfo.setImageStatus("2");
|
|||
|
|
greenImageInfo.setImageName(uuid+".jpg");
|
|||
|
|
// 执行保存
|
|||
|
|
greenimageinfoBizc.add(greenImageInfo);
|
|||
|
|
// 返回uuid
|
|||
|
|
return uuid;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static byte[] getRequestPostBytes(HttpServletRequest request) throws IOException {
|
|||
|
|
int contentLength = request.getContentLength();
|
|||
|
|
System.out.println("contentLength:"+contentLength);
|
|||
|
|
if (contentLength < 0) {
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
byte buffer[] = new byte[contentLength];
|
|||
|
|
for (int i = 0; i < contentLength;) {
|
|||
|
|
int readlen = request.getInputStream().read(buffer, i, contentLength - i);
|
|||
|
|
if (readlen == -1) {
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
i += readlen;
|
|||
|
|
}
|
|||
|
|
return buffer;
|
|||
|
|
}
|
|||
|
|
}
|