Commit 99045539 authored by zhuangzhuang's avatar zhuangzhuang

5.24--图片上传工具类

parent 1c10b076
...@@ -69,6 +69,10 @@ task copyJar(type:Copy){ ...@@ -69,6 +69,10 @@ task copyJar(type:Copy){
into ('build/libs/lib') into ('build/libs/lib')
} }
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
task release(type: Copy,dependsOn: [build,copyJar]) { task release(type: Copy,dependsOn: [build,copyJar]) {
// from 'conf' // from 'conf'
// into ('build/libs/core/conf') // into ('build/libs/core/conf')
......
package com.example.tdl.domain.vo;
public class DeviceVo {
private String warehouseNo;
private String tdl;
private String tdlSN;
private String SN;
private String type;
private String device;
public String getWarehouseNo() {
return warehouseNo;
}
public void setWarehouseNo(String warehouseNo) {
this.warehouseNo = warehouseNo;
}
public String getTdl() {
return tdl;
}
public void setTdl(String tdl) {
this.tdl = tdl;
}
public String getTdlSN() {
return tdlSN;
}
public void setTdlSN(String tdlSN) {
this.tdlSN = tdlSN;
}
public String getSN() {
return SN;
}
public void setSN(String SN) {
this.SN = SN;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getDevice() {
return device;
}
public void setDevice(String device) {
this.device = device;
}
}
package com.example.tdl.domain.vo;
public class SimpleWarehouseVo {
private String warehouseNo;
private String warehouseName;
private String regions;
private Integer count;
public String getWarehouseNo() {
return warehouseNo;
}
public void setWarehouseNo(String warehouseNo) {
this.warehouseNo = warehouseNo;
}
public String getWarehouseName() {
return warehouseName;
}
public void setWarehouseName(String warehouseName) {
this.warehouseName = warehouseName;
}
public String getRegions() {
return regions;
}
public void setRegions(String regions) {
this.regions = regions;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
}
...@@ -17,7 +17,9 @@ public interface WarehouseMapper { ...@@ -17,7 +17,9 @@ public interface WarehouseMapper {
List<ResultWarehouseVo> getAllByTerm(WarehouseTermVo warehouseTermVo); List<ResultWarehouseVo> getAllByTerm(WarehouseTermVo warehouseTermVo);
List<String> getByWarehouse(WareHouseByTermVo wareHouseByTermVo); List<SimpleWarehouseVo> getByWarehouse(WareHouseByTermVo wareHouseByTermVo);
List<DeviceVo> getDevice(String warehouseNo);
Integer getWarehouseNo(String warehouseNo); Integer getWarehouseNo(String warehouseNo);
......
...@@ -23,10 +23,14 @@ public class WarehouseService { ...@@ -23,10 +23,14 @@ public class WarehouseService {
return warehouseMapper.getAllByTerm(warehouseTermVo); return warehouseMapper.getAllByTerm(warehouseTermVo);
} }
public List<String> getByWarehouse(WareHouseByTermVo wareHouseByTermVo){ public List<SimpleWarehouseVo> getByWarehouse(WareHouseByTermVo wareHouseByTermVo){
return warehouseMapper.getByWarehouse(wareHouseByTermVo); return warehouseMapper.getByWarehouse(wareHouseByTermVo);
} }
public List<DeviceVo> getDevice(String warehouseNo){
return warehouseMapper.getDevice(warehouseNo);
}
public Integer getWarehouseNo(String warehouseNo){ public Integer getWarehouseNo(String warehouseNo){
return warehouseMapper.getWarehouseNo(warehouseNo); return warehouseMapper.getWarehouseNo(warehouseNo);
} }
......
...@@ -171,13 +171,13 @@ public class AliyunOSSClientUtil { ...@@ -171,13 +171,13 @@ public class AliyunOSSClientUtil {
} }
public static String uploadImg2Oss(OSSClient ossClient, MultipartFile file, String workNumber) { public static String uploadImg2Oss(OSSClient ossClient, MultipartFile file, String number,String imageType ) {
String originalFilename = file.getOriginalFilename(); String originalFilename = file.getOriginalFilename();
String name = workNumber + originalFilename.substring(originalFilename.lastIndexOf(".")).toLowerCase(); String name = number + originalFilename.substring(originalFilename.lastIndexOf(".")).toLowerCase();
try { try {
InputStream inputStream = file.getInputStream(); InputStream inputStream = file.getInputStream();
uploadFile2OSS(ossClient,inputStream, name); uploadFile2OSS(ossClient,inputStream, name,imageType);
return name; return name;
} catch (Exception e) { } catch (Exception e) {
logger.info("图片上传失败!"); logger.info("图片上传失败!");
...@@ -234,6 +234,44 @@ public class AliyunOSSClientUtil { ...@@ -234,6 +234,44 @@ public class AliyunOSSClientUtil {
} }
public static String uploadFile2OSS(OSSClient ossClient,InputStream instream, String fileName,String imageType ) {
String ret = "";
try {
//创建上传Object的Metadata
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentLength(instream.available());
objectMetadata.setCacheControl("no-cache");
objectMetadata.setContentType("image/jpeg");
objectMetadata.setContentDisposition("inline;filename=" + fileName);
//上传文件
String key = fileName;
if("user".equals(imageType)){
key = FOLDER1+key;
}else if("company".equals(imageType)){
key = FOLDER2+key;
}else if("palte".equals(imageType)){
key = FOLDER3+key;
}else if ("wareHouse".equals(imageType)){
key = FOLDER4+key;
}else if("circuit".equals(imageType)){
key = FOLDER4+key;
}
PutObjectResult putResult = ossClient.putObject(BACKET_NAME, key, instream, objectMetadata);
ret = putResult.getETag();
} catch (IOException e) {
logger.error(e.getMessage(), e);
} finally {
try {
if (instream != null) {
instream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return ret;
}
/** /**
* 创建模拟文件夹 * 创建模拟文件夹
* @param ossClient oss连接 * @param ossClient oss连接
......
...@@ -18,13 +18,13 @@ public class OSSClientConstants { ...@@ -18,13 +18,13 @@ public class OSSClientConstants {
//阿里云API的文件夹名称 //阿里云API的文件夹名称
public static final String FOLDER1="everyData/";//每日设备数据 public static final String FOLDER1="pic/userImage/";//用户头像
public static final String FOLDER2="picture/"; //图片上传 public static final String FOLDER2="pic/companyLog/"; //公司Log
public static final String FOLDER3="historyData/probesData/";//探点数据 public static final String FOLDER3="pic/palteImage/";//车辆照片
public static final String FOLDER4="tdlAlarmData/";//探点数据 public static final String FOLDER4="pic/wareHouseImage/";//仓库图片
//公共读写 //公共读写
private static final CannedAccessControlList acl_pub_readwrite = CannedAccessControlList.PublicReadWrite; private static final CannedAccessControlList acl_pub_readwrite = CannedAccessControlList.PublicReadWrite;
......
package com.example.tdl.util;
import com.aliyun.oss.OSSClient;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;
public class PicUploadUtil {
private static final String[] IMAGE_TYPE = new String[]{ ".bmp", ".jpg", ".jpeg", ".gif", ".png" };
public String uploadImage(MultipartFile multipartFile,String number,String imageType){
boolean isLegal = false;
// 判断文件后缀名是否符合
for (String type : IMAGE_TYPE) {
if (StringUtils.endsWithIgnoreCase(multipartFile.getOriginalFilename(),type)) {
isLegal = true;
break;
}
}
if(isLegal) {
try {
//初始化OSSClient
OSSClient ossClient = AliyunOSSClientUtil.getOSSClient();
String url = AliyunOSSClientUtil.uploadImg2Oss(ossClient, multipartFile, number,imageType);
if (url == null) {
return "上传失败";
} else {
if("user".equals(imageType)){
return "https://uploadexcelhltp.oss-cn-shanghai.aliyuncs.com/pic/userImage/" + url;
}else if("company".equals(imageType)){
return "https://uploadexcelhltp.oss-cn-shanghai.aliyuncs.com/pic/companyLog/" + url;
}else if("palte".equals(imageType)){
return "https://uploadexcelhltp.oss-cn-shanghai.aliyuncs.com/pic/palteImage/" + url;
}else if ("wareHouse".equals(imageType)){
return "https://uploadexcelhltp.oss-cn-shanghai.aliyuncs.com/pic/wareHouseImage/" + url;
}else if("circuit".equals(imageType)){
return "https://uploadexcelhltp.oss-cn-shanghai.aliyuncs.com/picture/" + url;
}
}
} catch (Exception e) {
e.printStackTrace();
return "上传失败";
}
}else{
return "图片格式不正确";
}
return "上传失败";
}
}
...@@ -23,6 +23,7 @@ import javax.servlet.http.HttpServletRequest; ...@@ -23,6 +23,7 @@ import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
@RestController @RestController
...@@ -95,7 +96,7 @@ public class WarehouseController { ...@@ -95,7 +96,7 @@ public class WarehouseController {
" city:市," + " city:市," +
" property:属性," + " property:属性," +
" type:,仓库类型" + " type:,仓库类型" +
" hardwareLevel:硬件等级。" + " hardwareLevel:硬件等级。(查询全部传{})" +
"返回值说明:" + "返回值说明:" +
" warehouseName:仓库名," + " warehouseName:仓库名," +
" warehouseNo:仓库编号," + " warehouseNo:仓库编号," +
...@@ -134,6 +135,18 @@ public class WarehouseController { ...@@ -134,6 +135,18 @@ public class WarehouseController {
//获取仓库以及设备数据 //获取仓库以及设备数据
@ApiOperation(value = "获取仓库以及设备数据",notes = "获取仓库以及设备数据,传值说明:" +
" warehouseName:仓库名," +
" country:国家," +
" city:市," +
" alarm:预警(查询全部传{})" +
"返回值:"+
" warehouseName:仓库名," +
" warehouseNo:仓库编号," +
" regions:国家地区," +
" counts:设备数量," +
" temp:当前温度," +
" humidity:当前温度" )
@ApiImplicitParams({ @ApiImplicitParams({
@ApiImplicitParam(paramType="header", name = "Account_token", value = "token", required = true, dataType = "String"), @ApiImplicitParam(paramType="header", name = "Account_token", value = "token", required = true, dataType = "String"),
}) })
...@@ -143,9 +156,26 @@ public class WarehouseController { ...@@ -143,9 +156,26 @@ public class WarehouseController {
String datum = tokenRedisService.get("TOKEN_" +token); String datum = tokenRedisService.get("TOKEN_" +token);
UserRedisVo userRedisVo = gson.fromJson(datum,UserRedisVo.class); UserRedisVo userRedisVo = gson.fromJson(datum,UserRedisVo.class);
warehouseTermVo.setCompanyName(userRedisVo.getCompanyName()); warehouseTermVo.setCompanyName(userRedisVo.getCompanyName());
//获取 List<SimpleWarehouseVo> simpleWarehouseVos = warehouseService.getByWarehouse(warehouseTermVo);
warehouseService.getByWarehouse(warehouseTermVo); for( int i = 0,length= simpleWarehouseVos.size();i<length;i++ ){
return gson.toJson(fb); //判断是否已经绑定设备
if(simpleWarehouseVos.get(i).getCount() == 0){
continue;
}
List<DeviceVo> list = warehouseService.getDevice(simpleWarehouseVos.get(i).getWarehouseNo());
if(list.size() == 0){
continue;
}
String sql = "";
for(int j = 0,len=list.size();j<len;j++){
if( StringUtils.isEmpty(list.get(i).getTdlSN())){
continue;
}
}
}
return gson.toJson(simpleWarehouseVos);
} }
//根据仓库编号获取仓库信息 //根据仓库编号获取仓库信息
......
...@@ -206,4 +206,41 @@ ...@@ -206,4 +206,41 @@
AND a.alarmScene=#{alarmScene,jdbcType=VARCHAR} AND a.alarmScene=#{alarmScene,jdbcType=VARCHAR}
and c.companyName=#{companyName,jdbcType=VARCHAR} and c.companyName=#{companyName,jdbcType=VARCHAR}
</select> </select>
<select id="getByWarehouse" parameterType="com.example.tdl.domain.vo.WareHouseByTermVo" resultType="com.example.tdl.domain.vo.SimpleWarehouseVo">
select warehouseNo, warehouseName,CONCAT_WS("-",c.country,c.city) regions, (select count(id) from tdldevice t where t.warehouse_id = w.id ) count
from warehouse w ,company c ,city ct
where c.id=w.company_id
and ct.id = w.city_id
and c.companyName = #{companyName,jdbcType=VARCHAR}
<if test="warehouseName!=null">
AND warehouseName like CONCAT(CONCAT('%',#{warehouseName,jdbcType=VARCHAR}), '%')
</if>
<if test="country!=null">
AND ct.country like CONCAT(CONCAT('%',#{country,jdbcType=VARCHAR}), '%')
</if>
<if test="city!=null">
AND ct.city like CONCAT(CONCAT('%',#{city,jdbcType=VARCHAR}), '%')
</if>
<if test="property!=null">
AND property=#{property,jdbcType=VARCHAR}
</if>
<if test="type!=null">
AND type=#{type,jdbcType=VARCHAR}
</if>
<if test="hardwareLevel!=null">
AND hardwareLevel=#{hardwareLevel,jdbcType=VARCHAR}
</if>
</select>
<select id="getDevice" parameterType="String" resultType="com.example.tdl.domain.vo.DeviceVo">
select w.warehouseNo,CONCAT("TDL-",t.TDLSN) tdl,t.TDLSN,g.SN,g.type,CONCAT(g.type,"_",g.SN) device
from gateway_warehouse gw,gateway g,tdldevice t,warehouse w,
where gw.warehouse_id = w.id
and gw.gateway_id = g.id
and t.warehouse_id = w.id
and t.gateway_id = g.id
and w.warehouseNo= #{warehouseNo,jdbcType=VARCHAR}
</select>
</mapper> </mapper>
\ No newline at end of file
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