Commit df7fd2bc authored by zhangyong's avatar zhangyong

1

parent c9a5aceb
package com.egolm.admin.controller;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
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.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.alibaba.fastjson.JSONObject;
import com.egolm.admin.pojo.TSystemCtrl;
import com.egolm.admin.service.OSSFileService;
import com.egolm.admin.service.SystemCtlService;
import com.egolm.admin.util.AdminContstrant;
import com.egolm.common.DateUtil;
import com.egolm.common.FileUtil;
import com.egolm.common.HttpUtil;
import com.egolm.common.bean.Rjx;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
/**
*
* @ClassName: OSSFileController
* @Description:文件、图片处理接口
* @author: zhang.yong
* @date: 2018年7月23日 上午10:53:38
*
*/
@Api(tags={"文件、图片处理接口"})
@RestController
@RequestMapping("oss")
public class OSSFileController {
private static final Log logger = LogFactory.getLog(OSSFileController.class);
@Autowired
private OSSFileService oSSFileService;
@Autowired
private SystemCtlService systemCtlService;
/**
* http://qa.egolm.com/mgr/doc.html
* @Title: upload
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param: @param request
* @param: @return
* @param: @throws IOException
* @return: String
* @throws
*/
@ApiOperation("上传(文件、图片)")
@ApiImplicitParams({
})
@RequestMapping(value = "/upload",method=RequestMethod.POST) //
public String upload(@RequestParam("file") MultipartFile file,HttpServletRequest request) throws IOException {
String[] codes = new String[2];
codes[0] = AdminContstrant.OSS_UPLOAD_CONFIG_KEY;
codes[1] = AdminContstrant.OSS_UPLOAD_SECRET_KEY;
List<TSystemCtrl> listCtl = systemCtlService.getTSystemCtrlByCode(codes);
if(listCtl.isEmpty() || listCtl.size() !=2 ) {
return Rjx.jsonErr().setCode(-100).setMessage("请检查"+codes[0]+ "和"+codes[1]+" 参数是否配置").toJson();
}
String ossUrl = ""; //上传的URL
String httpUrl = ""; //图片访问的URL
String dir = ""; //一级目录为document ,此为二级目录
String documentID = ""; // document 目录的ID
String appID = ""; // 自建OSS平台分配的 appid
String secret = ""; // 自建OSS平台分配的 密钥
for(TSystemCtrl ctrl:listCtl) {
String code = ctrl.getsCode();
if(code.equals(codes[0])) {
ossUrl = ctrl.getsValue1();
httpUrl = ctrl.getsValue2();
documentID = ctrl.getsValue3();
dir = ctrl.getsValue4();
}
if(code.equals(codes[1])) {
appID = ctrl.getsValue1();
secret = ctrl.getsValue2();
}
}
Map<String,String> ossMap = new HashMap<String,String>(); //签名用的参数
ossMap.put("appID", appID);
ossMap.put("secret", secret);
Map<String, Object> params = new TreeMap<String,Object>(); //调用创建目录
params.put("sParentID", documentID);
params.put("dir", dir+"/"+DateUtil.format(new Date(),DateUtil.FMTDATE));
Map<String,String> headers = oSSFileService.createSign(params,ossMap);
String createResult = HttpUtil.post(ossUrl+"mk", params, headers);
JSONObject createObj = JSONObject.parseObject(createResult);
logger.info("创建目录结果:"+createObj);
if(createObj.getInteger("status") == 200) {
String parentID = createObj.getJSONObject("data").getString("sFileID");
logger.info("目录ID---"+parentID);
Map<String,Object> uploadParams = new TreeMap<String,Object>(); //参数
uploadParams.put("isRename", true);
uploadParams.put("sParentID", parentID);
Map<String,String> uploadHeaders = oSSFileService.createSign(uploadParams,ossMap); //签名
logger.info("上传的参数--"+uploadParams);
logger.info("上传的签名 ---"+uploadHeaders);
//将对象转换成file
String fileName = file.getName();
String prefix =fileName.substring(fileName.lastIndexOf(".") + 1).toUpperCase(); //获取后缀名
File uploadFile = null;
InputStream ins = file.getInputStream();
uploadFile=new File(file.getOriginalFilename());
FileUtil.inputStreamToFile(ins, uploadFile);
System.out.println("新的file对象-----"+uploadFile.getPath()+"------"+uploadFile.getName());
String contentType = FileUtil.contentType(uploadFile);
System.out.println("文件上传的 contentType 为 "+contentType);
Map<String,File> attachments = new HashMap<String,File>();
attachments.put("binaryData", uploadFile);
String uploadResult = HttpUtil.post(ossUrl+"upload", uploadParams, uploadHeaders,attachments,contentType);
logger.info("上传文件结果--"+uploadResult);
uploadFile.delete();
JSONObject uploadObj = JSONObject.parseObject(uploadResult);
int status = uploadObj.getInteger("status");
if(status == 200) {
String filePath = uploadObj.getJSONArray("data").getJSONObject(0).getString("sFilePath");
return Rjx.jsonOk().set("filePath",httpUrl+""+filePath).toJson();
}else {
return Rjx.json().setCode(status).setMessage(uploadObj.getString("message")).toJson();
}
}else {
return Rjx.json().setCode(createObj.getInteger("status")).setMessage(createObj.getString("message")).toJson();
}
}
@ApiOperation("测试上传")
@ApiImplicitParams({
@ApiImplicitParam(paramType = "query", name = "uFilePath", dataType = "String", required = false, value = "绝对路径", defaultValue = ""),
})
@RequestMapping(value = "/testUpload",method=RequestMethod.POST) //
public String testUpload(HttpServletRequest request) throws IOException {
String uFilePath = request.getParameter("uFilePath");
String[] codes = new String[2];
codes[0] = AdminContstrant.OSS_UPLOAD_CONFIG_KEY;
codes[1] = AdminContstrant.OSS_UPLOAD_SECRET_KEY;
List<TSystemCtrl> listCtl = systemCtlService.getTSystemCtrlByCode(codes);
if(listCtl.isEmpty() || listCtl.size() !=2 ) {
return Rjx.jsonErr().setCode(-100).setMessage("请检查"+codes[0]+ "和"+codes[1]+" 参数是否配置").toJson();
}
String ossUrl = ""; //上传的URL
String httpUrl = ""; //图片访问的URL
String dir = ""; //一级目录为document ,此为二级目录
String documentID = ""; // document 目录的ID
String appID = ""; // 自建OSS平台分配的 appid
String secret = ""; // 自建OSS平台分配的 密钥
for(TSystemCtrl ctrl:listCtl) {
String code = ctrl.getsCode();
if(code.equals(codes[0])) {
ossUrl = ctrl.getsValue1();
httpUrl = ctrl.getsValue2();
documentID = ctrl.getsValue3();
dir = ctrl.getsValue4();
}
if(code.equals(codes[1])) {
appID = ctrl.getsValue1();
secret = ctrl.getsValue2();
}
}
Map<String,String> ossMap = new HashMap<String,String>(); //签名用的参数
ossMap.put("appID", appID);
ossMap.put("secret", secret);
Map<String, Object> params = new TreeMap<String,Object>(); //调用创建目录
params.put("sParentID", documentID);
params.put("dir", dir+"/"+DateUtil.format(new Date(),DateUtil.FMTDATE));
Map<String,String> headers = oSSFileService.createSign(params,ossMap);
String createResult = HttpUtil.post(ossUrl+"mk", params, headers);
JSONObject createObj = JSONObject.parseObject(createResult);
logger.info("创建目录结果:"+createObj);
if(createObj.getInteger("status") == 200) {
String parentID = createObj.getJSONObject("data").getString("sFileID");
logger.info("目录ID---"+parentID);
Map<String,Object> uploadParams = new TreeMap<String,Object>(); //参数
uploadParams.put("isRename", true);
uploadParams.put("sParentID", parentID);
Map<String,String> uploadHeaders = oSSFileService.createSign(uploadParams,ossMap); //签名
logger.info("上传的参数--"+uploadParams);
logger.info("上传的签名 ---"+uploadHeaders);
//将对象转换成file
File uploadFile = new File(uFilePath);
String contentType = FileUtil.contentType(uploadFile);
System.out.println("文件上传的 contentType 为 "+contentType);
Map<String,File> attachments = new HashMap<String,File>();
attachments.put("binaryData", uploadFile);
String uploadResult = HttpUtil.post(ossUrl+"upload", uploadParams, uploadHeaders,attachments,contentType);
logger.info("上传文件结果--"+uploadResult);
uploadFile.delete();
JSONObject uploadObj = JSONObject.parseObject(uploadResult);
int status = uploadObj.getInteger("status");
if(status == 200) {
String filePath = uploadObj.getJSONArray("data").getJSONObject(0).getString("sFilePath");
return Rjx.jsonOk().set("filePath",httpUrl+""+filePath).toJson();
}else {
return Rjx.json().setCode(status).setMessage(uploadObj.getString("message")).toJson();
}
}else {
return Rjx.json().setCode(createObj.getInteger("status")).setMessage(createObj.getString("message")).toJson();
}
}
}
package com.egolm.admin.pojo;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.util.Date;
/**
* @author 曲欣亮
* @since 2018-08-06
* @version v-1.0
*/
@Entity(name="tSystemCtrl")
public class TSystemCtrl implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private String sCreateUser;
private String sChangeUser;
private String sSysTypeID;
private Date dConfirmDate;
private String sValue2;
private String sValue3;
@Id
private String sCode;
private Date dCreateDate;
private String sSysType;
private Date dLastUpdateTime;
private String sValue1;
private Date dChangeDate;
private String sDesc;
private String sValue4;
private String sConfirmUser;
private String sMemo;
private Integer nTag;
/**
* 无参数构造方法
*/
public TSystemCtrl () {
super();
}
/**
* 全参数构造方法
* @param sCreateUser
* @param sChangeUser
* @param sSysTypeID
* @param dConfirmDate
* @param sValue2
* @param sValue3
* @param sCode
* @param dCreateDate
* @param sSysType
* @param dLastUpdateTime
* @param sValue1
* @param dChangeDate
* @param sDesc
* @param sValue4
* @param sConfirmUser
* @param sMemo
* @param nTag
*/
public TSystemCtrl(String sCreateUser, String sChangeUser, String sSysTypeID, Date dConfirmDate, String sValue2, String sValue3, String sCode, Date dCreateDate, String sSysType, Date dLastUpdateTime, String sValue1, Date dChangeDate, String sDesc, String sValue4, String sConfirmUser, String sMemo, Integer nTag) {
this.sCreateUser = sCreateUser;
this.sChangeUser = sChangeUser;
this.sSysTypeID = sSysTypeID;
this.dConfirmDate = dConfirmDate;
this.sValue2 = sValue2;
this.sValue3 = sValue3;
this.sCode = sCode;
this.dCreateDate = dCreateDate;
this.sSysType = sSysType;
this.dLastUpdateTime = dLastUpdateTime;
this.sValue1 = sValue1;
this.dChangeDate = dChangeDate;
this.sDesc = sDesc;
this.sValue4 = sValue4;
this.sConfirmUser = sConfirmUser;
this.sMemo = sMemo;
this.nTag = nTag;
}
public void setsCreateUser(String sCreateUser) {
this.sCreateUser = sCreateUser;
}
public String getsCreateUser() {
return sCreateUser;
}
public void setsChangeUser(String sChangeUser) {
this.sChangeUser = sChangeUser;
}
public String getsChangeUser() {
return sChangeUser;
}
public void setsSysTypeID(String sSysTypeID) {
this.sSysTypeID = sSysTypeID;
}
public String getsSysTypeID() {
return sSysTypeID;
}
public void setdConfirmDate(Date dConfirmDate) {
this.dConfirmDate = dConfirmDate;
}
public Date getdConfirmDate() {
return dConfirmDate;
}
public void setsValue2(String sValue2) {
this.sValue2 = sValue2;
}
public String getsValue2() {
return sValue2;
}
public void setsValue3(String sValue3) {
this.sValue3 = sValue3;
}
public String getsValue3() {
return sValue3;
}
public void setsCode(String sCode) {
this.sCode = sCode;
}
public String getsCode() {
return sCode;
}
public void setdCreateDate(Date dCreateDate) {
this.dCreateDate = dCreateDate;
}
public Date getdCreateDate() {
return dCreateDate;
}
public void setsSysType(String sSysType) {
this.sSysType = sSysType;
}
public String getsSysType() {
return sSysType;
}
public void setdLastUpdateTime(Date dLastUpdateTime) {
this.dLastUpdateTime = dLastUpdateTime;
}
public Date getdLastUpdateTime() {
return dLastUpdateTime;
}
public void setsValue1(String sValue1) {
this.sValue1 = sValue1;
}
public String getsValue1() {
return sValue1;
}
public void setdChangeDate(Date dChangeDate) {
this.dChangeDate = dChangeDate;
}
public Date getdChangeDate() {
return dChangeDate;
}
public void setsDesc(String sDesc) {
this.sDesc = sDesc;
}
public String getsDesc() {
return sDesc;
}
public void setsValue4(String sValue4) {
this.sValue4 = sValue4;
}
public String getsValue4() {
return sValue4;
}
public void setsConfirmUser(String sConfirmUser) {
this.sConfirmUser = sConfirmUser;
}
public String getsConfirmUser() {
return sConfirmUser;
}
public void setsMemo(String sMemo) {
this.sMemo = sMemo;
}
public String getsMemo() {
return sMemo;
}
public void setnTag(Integer nTag) {
this.nTag = nTag;
}
public Integer getnTag() {
return nTag;
}
}
package com.egolm.admin.service;
import java.util.Map;
public interface OSSFileService {
public Map<String,String> createSign(Map<String, Object> params,Map<String,String> ossMap);
}
package com.egolm.admin.service;
import java.util.List;
import java.util.Map;
import com.egolm.admin.pojo.TSystemCtrl;
public interface SystemCtlService {
public List<TSystemCtrl> getTSystemCtrlByCode(String[] codes);
}
package com.egolm.admin.service.impl;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Hex;
import org.springframework.stereotype.Service;
import com.egolm.admin.service.OSSFileService;
/**
*
* @ClassName: OSSFileServiceImpl
* @Description:图片处理服务
* @author: zhang.yong
* @date: 2018年7月23日 上午11:13:04
*
*/
@Service
public class OSSFileServiceImpl implements OSSFileService {
/**
* 生成自建OSS系统 所需要的签名
* <p>Title: createSign</p>
* <p>Description: </p>
* @param params 请求参数
* @param ossMap 计算签名的appid 和密钥
* @return
* @see com.egolm.admin.service.msc.manager.service.OSSFileService#createSign(java.util.Map, java.util.Map)
*/
@Override
public Map<String,String> createSign(Map<String, Object> params,Map<String,String> ossMap) {
Map<String,String> headers = new HashMap<String,String>();
try {
String appid = ossMap.get("appID")+"";
String secret =ossMap.get("secret")+"";
StringBuffer sb = new StringBuffer();
for(String key : params.keySet()) {
String val = params.get(key)+"";
sb.append(key).append("=").append(val).append("&");
}
String queryString = sb.toString();
if(queryString.endsWith("&")) {
queryString = queryString.substring(0, queryString.length() - 1);
}
SecretKey secretKey = new SecretKeySpec(secret.getBytes(), "HmacSHA1");
Mac mac = Mac.getInstance(secretKey.getAlgorithm());
mac.init(secretKey);
String text = appid + queryString + appid;
byte[] bs = mac.doFinal(text.getBytes());
String sign = Hex.encodeHexString(bs);
headers.put("appid", appid);
headers.put("sign", sign);
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
}
return headers;
}
}
package com.egolm.admin.service.impl;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import com.egolm.admin.pojo.TSystemCtrl;
import com.egolm.admin.service.SystemCtlService;
import com.egolm.common.jdbc.JdbcTemplate;
/**
* a
* @ClassName: SystemCtlServiceImpl
* @Description:获取参数配置
* @author: zhang.yong
* @date: 2018年8月6日 下午4:20:14
*
*/
@Service
public class SystemCtlServiceImpl implements SystemCtlService {
@Autowired
@Qualifier("shopJdbcTemplate")
private JdbcTemplate jdbcTemplate;
@Override
public List<TSystemCtrl> getTSystemCtrlByCode(String[] codes) {
String sql = "select * from tSystemCtrl where sCode in ? and nTag&1=0";
return jdbcTemplate.queryForList(sql, TSystemCtrl.class, codes);
}
}
package com.egolm.admin.util;
/**
*
* @ClassName: AdminContstrant
* @Description:常量
* @author: zhang.yong
* @date: 2018年8月6日 下午4:36:16
*
*/
public class AdminContstrant {
public static final String OSS_UPLOAD_CONFIG_KEY = "OSS_UPLOAD_CONFIG"; //自建OSS上传参数配置
public static final String OSS_UPLOAD_SECRET_KEY = "OSS_UPLOAD_SECRET"; //自建OSS上传密钥,算签名的
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment