Commit d6baf64d authored by hanfeng's avatar hanfeng

app更新包上传接口

parent 68c5376c
//package com.xxfc.platform.app.utils;
//
//import lombok.extern.slf4j.Slf4j;
//
//import java.io.*;
//import java.net.*;
//import java.util.Map;
//import java.util.UUID;
//@Slf4j
//public class UploadUtil {
//
//
// public static final String TAG = UploadUtil.class.getName();
//
// private static final String CHARSET = "utf-8"; // 设置编码
// private static final int TIME_OUT = 8*1000;
// public static String result = null;
// /**
// * Android上传文件到服务端
// *
// * @param file 需要上传的文件
// * @param RequestURL 请求的url
// * @return 返回响应的内容
// */
// public static String uploadFile(final File file, final String RequestURL) {
//
//// Thread thread = new Thread(new Runnable() {
//// @Override
//// public void run() {
// String result = null;
// String BOUNDARY = UUID.randomUUID().toString(); // 边界标识 随机生成
// String PREFIX = "--", LINE_END = "\r\n";
// String CONTENT_TYPE = "multipart/form-data"; // 内容类型
// try {
// URL url = new URL(RequestURL);
// HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//
// conn.setReadTimeout(TIME_OUT);
// conn.setConnectTimeout(TIME_OUT);
// conn.setDoInput(true); // 允许输入流
// conn.setDoOutput(true); // 允许输出流
// conn.setUseCaches(false); // 不允许使用缓存
// conn.setRequestMethod("POST"); // 请求方式
// conn.setRequestProperty("Charset", CHARSET); // 设置编码
// conn.setRequestProperty("connection", "keep-alive");
// conn.setRequestProperty("Content-Type", CONTENT_TYPE + ";boundary=" + BOUNDARY);
//
//
// if (file != null) {
// /**
// * 当文件不为空,把文件包装并且上传
// */
// DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
// StringBuffer sb = new StringBuffer();
// sb.append(PREFIX);
// sb.append(BOUNDARY);
// sb.append(LINE_END);
// /**
// * 这里重点注意: name里面的值为服务端需要key 只有这个key 才可以得到对应的文件
// * filename是文件的名字,包含后缀名的 比如:abc.png
// */
//
// sb.append("Content-Disposition: form-data; name=\"upload\"; filename=\""
// + file.getName() + "\"" + LINE_END);
// sb.append("Content-Type: application/octet-stream; charset=" + CHARSET + LINE_END);
// sb.append(LINE_END);
// dos.write(sb.toString().getBytes());
// InputStream is = new FileInputStream(file);
// byte[] bytes = new byte[1024];
// int len = 0;
// while ((len = is.read(bytes)) != -1) {
// dos.write(bytes, 0, len);
// }
// is.close();
// dos.write(LINE_END.getBytes());
// byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINE_END).getBytes();
// dos.write(end_data);
// dos.flush();
// /**
// * 获取响应码 200=成功 当响应成功,获取响应的流
// */
// int res = conn.getResponseCode();
// LOG.i(TAG, "response code:" + res);
// if(res==200)
// {
// LOG.i(TAG, "request success");
// InputStream input = conn.getInputStream();
// StringBuffer sb1 = new StringBuffer();
// int ss;
// while ((ss = input.read()) != -1) {
// sb1.append((char) ss);
// }
// result = sb1.toString();
// UploadUtil.result = new String(result.getBytes("iso-8859-1"),"utf-8");
// LOG.i(TAG, "result : " + UploadUtil.result);
// input.close();
// }
// else{
// LOG.e(TAG, "request error");
// }
//
// }
// } catch (MalformedURLException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// } catch (UnsupportedEncodingException e) {
// e.printStackTrace();
// } catch (ProtocolException e) {
// e.printStackTrace();
// } catch (MalformedURLException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// }
//
//// }
//// });
//// thread.start();
// LOG.i(TAG, "---"+UploadUtil.result+"---");
// return UploadUtil.result;
// }
//
//
// /**
// * 通过拼接的方式构造请求内容,实现参数传输以及文件传输
// *
// * @param url Service net address
// * @param params text content
// * @param files pictures
// * @return String result of Service response
// * @throws IOException
// */
// public static String post(String url, Map<String, String> params, Map<String, File> files)
// throws IOException {
// String BOUNDARY = java.util.UUID.randomUUID().toString();
// String PREFIX = "--", LINEND = "\r\n";
// String MULTIPART_FROM_DATA = "multipart/form-data";
// String CHARSET = "UTF-8";
//
//
// URL uri = new URL(url);
// HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
// conn.setReadTimeout(10 * 1000); // 缓存的最长时间
// conn.setDoInput(true);// 允许输入
// conn.setDoOutput(true);// 允许输出
// conn.setUseCaches(false); // 不允许使用缓存
// conn.setRequestMethod("POST");
// conn.setRequestProperty("connection", "keep-alive");
// conn.setRequestProperty("Charsert", "UTF-8");
// conn.setRequestProperty("Content-Type", MULTIPART_FROM_DATA + ";boundary=" + BOUNDARY);
//
//
// // 首先组拼文本类型的参数
// StringBuilder sb = new StringBuilder();
// for (Map.Entry<String, String> entry : params.entrySet()) {
// sb.append(PREFIX);
// sb.append(BOUNDARY);
// sb.append(LINEND);
// sb.append("Content-Disposition: form-data; name=\"" + entry.getKey() + "\"" + LINEND);
// sb.append("Content-Type: text/plain; charset=" + CHARSET + LINEND);
// sb.append("Content-Transfer-Encoding: 8bit" + LINEND);
// sb.append(LINEND);
// sb.append(entry.getValue());
// sb.append(LINEND);
// }
//
//
// DataOutputStream outStream = new DataOutputStream(conn.getOutputStream());
// outStream.write(sb.toString().getBytes());
// // 发送文件数据
// if (files != null)
// for (Map.Entry<String, File> file : files.entrySet()) {
// StringBuilder sb1 = new StringBuilder();
// sb1.append(PREFIX);
// sb1.append(BOUNDARY);
// sb1.append(LINEND);
// sb1.append("Content-Disposition: form-data; name=\"upload\"; filename=\""
// + file.getValue().getName() + "\"" + LINEND);
// sb1.append("Content-Type: application/octet-stream; charset=" + CHARSET + LINEND);
// sb1.append(LINEND);
// outStream.write(sb1.toString().getBytes());
//
//
// InputStream is = new FileInputStream(file.getValue());
// byte[] buffer = new byte[1024];
// int len = 0;
// while ((len = is.read(buffer)) != -1) {
// outStream.write(buffer, 0, len);
// }
//
//
// is.close();
// outStream.write(LINEND.getBytes());
// }
//
//
// // 请求结束标志
// byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes();
// outStream.write(end_data);
// outStream.flush();
// // 得到响应码
// int res = conn.getResponseCode();
// InputStream in = conn.getInputStream();
// StringBuilder sb2 = new StringBuilder();
// if (res == 200) {
// int ch;
// while ((ch = in.read()) != -1) {
// sb2.append((char) ch);
// }
// }
// outStream.close();
// conn.disconnect();
// return sb2.toString();
// }
//
//}
package com.xxfc.platform.app.biz; package com.xxfc.platform.app.biz;
import com.github.wxiaoqi.security.auth.client.annotation.IgnoreUserToken;
import com.github.wxiaoqi.security.common.constant.RestCode; import com.github.wxiaoqi.security.common.constant.RestCode;
import com.github.wxiaoqi.security.common.msg.ObjectRestResponse; import com.github.wxiaoqi.security.common.msg.ObjectRestResponse;
import com.github.wxiaoqi.security.common.util.process.ResultCode; import com.github.wxiaoqi.security.common.util.process.ResultCode;
import com.xxfc.platform.vehicle.common.RestResponse;
import com.xxfc.platform.vehicle.constant.RedisKey;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.xxfc.platform.app.entity.AppVersion; import com.xxfc.platform.app.entity.AppVersion;
import com.xxfc.platform.app.mapper.AppVersionMapper; import com.xxfc.platform.app.mapper.AppVersionMapper;
import com.github.wxiaoqi.security.common.biz.BaseBiz; import com.github.wxiaoqi.security.common.biz.BaseBiz;
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.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example; import tk.mybatis.mapper.entity.Example;
import java.io.File;
import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit;
/** /**
* *
...@@ -22,6 +44,12 @@ import java.util.List; ...@@ -22,6 +44,12 @@ import java.util.List;
@Service @Service
public class AppVersionBiz extends BaseBiz<AppVersionMapper,AppVersion> { public class AppVersionBiz extends BaseBiz<AppVersionMapper,AppVersion> {
@Value("${app.uploadPath}")
private String uploadPath;
@Autowired
private RedisTemplate redisTemplate;
public static final DateTimeFormatter DEFAULT_DATE_TIME_FORMATTER = DateTimeFormat.forPattern("yyyy-MM-dd");
public ObjectRestResponse getVersion(String version,Integer type){ public ObjectRestResponse getVersion(String version,Integer type){
if (StringUtils.isBlank(version)||type==null){ if (StringUtils.isBlank(version)||type==null){
...@@ -49,4 +77,41 @@ public class AppVersionBiz extends BaseBiz<AppVersionMapper,AppVersion> { ...@@ -49,4 +77,41 @@ public class AppVersionBiz extends BaseBiz<AppVersionMapper,AppVersion> {
} }
return ObjectRestResponse.succ(); return ObjectRestResponse.succ();
} }
public RestResponse uploadDrivingLicense(MultipartFile file) throws IOException {
//创建本日存放目录
DateTime now = DateTime.now();
String dirPathToday = File.separator + now.toString(DEFAULT_DATE_TIME_FORMATTER);
String redisNoKey = RedisKey.UPLOAD_FILE_NO_PREFIX + now.toString(DEFAULT_DATE_TIME_FORMATTER);
Long no = redisTemplate.opsForValue().increment(redisNoKey);
if(no.equals(1L)){
redisTemplate.expire(redisNoKey,1, TimeUnit.DAYS);
}
String fileName = file.getOriginalFilename();
String realFileRelPath = dirPathToday + File.separator + no + fileName.substring(fileName.lastIndexOf("."));
//文件存放路径
String filePath = uploadPath + realFileRelPath;
//将文件写入指定位置
FileUtils.copyInputStreamToFile(file.getInputStream(), new File(filePath));
return RestResponse.suc(realFileRelPath);
}
/**
* 下载行驶证图片
* @param realFileRelPath
* @return
* @throws Exception
*/
public ResponseEntity<byte[]> downloadInstallationPackage(String realFileRelPath) throws Exception{
String filePath = uploadPath + realFileRelPath;
File file = new File(filePath);//新建一个文件
HttpHeaders headers = new HttpHeaders();//http头信息
String downloadFileName = new String(file.getName());//设置编码
headers.setContentDispositionFormData("attachment", downloadFileName);
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers, HttpStatus.CREATED);
}
} }
\ No newline at end of file
...@@ -9,14 +9,22 @@ import com.github.wxiaoqi.security.common.util.process.ResultCode; ...@@ -9,14 +9,22 @@ import com.github.wxiaoqi.security.common.util.process.ResultCode;
import com.xxfc.platform.app.biz.AppVersionBiz; import com.xxfc.platform.app.biz.AppVersionBiz;
import com.xxfc.platform.app.entity.AppVersion; import com.xxfc.platform.app.entity.AppVersion;
import com.xxfc.platform.app.entity.Cofig; import com.xxfc.platform.app.entity.Cofig;
import com.xxfc.platform.vehicle.common.RestResponse;
import com.xxfc.platform.vehicle.constant.ResCode.ResCode;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.bind.annotation.RestController;
import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -24,8 +32,8 @@ import java.util.List; ...@@ -24,8 +32,8 @@ import java.util.List;
@RequestMapping("version") @RequestMapping("version")
@IgnoreClientToken @IgnoreClientToken
public class AppVersionController extends BaseController<AppVersionBiz,AppVersion> { public class AppVersionController extends BaseController<AppVersionBiz,AppVersion> {
//最大上传500MB
private Long MAX_DRIVING_LICENSE_SIZE =1024*1024*500L;
@ApiModelProperty("app自动更新") @ApiModelProperty("app自动更新")
@RequestMapping(value ="/app/unauth/info",method = RequestMethod.GET) @RequestMapping(value ="/app/unauth/info",method = RequestMethod.GET)
@IgnoreUserToken @IgnoreUserToken
...@@ -36,4 +44,31 @@ public class AppVersionController extends BaseController<AppVersionBiz,AppVersio ...@@ -36,4 +44,31 @@ public class AppVersionController extends BaseController<AppVersionBiz,AppVersio
return baseBiz.getVersion(version,type); return baseBiz.getVersion(version,type);
} }
@PostMapping(value = "/upload/installationPackage")
@ApiOperation(value = "上传app安装包")
public RestResponse uploadInstallationPackage(@RequestParam("file") MultipartFile file)
throws Exception {
Assert.notNull(file);
String contentType = file.getContentType(); //图片文件类型
// String fileName = file.getOriginalFilename(); //图片名字
if (!contentType.equals("apk") && !contentType.equals("ipa")) {
return RestResponse.code(ResCode.INVALID_REST_REQ_PARAM.getCode());
}
if (file.getSize() > MAX_DRIVING_LICENSE_SIZE) {
return RestResponse.code(ResCode.INVALID_REST_REQ_PARAM.getCode());
}
return baseBiz.uploadDrivingLicense(file);
}
@IgnoreUserToken
@GetMapping(value = "/download/installationPackage") //匹配的是href中的download请求
@ApiOperation(value = "下载app安装包")
public ResponseEntity<byte[]> downloadInstallationPackage(@RequestParam("realFileRelPath") String realFileRelPath) throws Exception {
return baseBiz.downloadInstallationPackage(realFileRelPath);
}
} }
\ No newline at end of file
package com.xxfc.platform.vehicle.entity; package com.xxfc.platform.vehicle.entity;
import lombok.Data; import lombok.Data;
import tk.mybatis.mapper.annotation.KeySql;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Id; import javax.persistence.Id;
......
...@@ -126,6 +126,7 @@ public class VehicleBiz extends BaseBiz<VehicleMapper, Vehicle> { ...@@ -126,6 +126,7 @@ public class VehicleBiz extends BaseBiz<VehicleMapper, Vehicle> {
* @throws Exception * @throws Exception
*/ */
public ResponseEntity<byte[]> downloadDrivingLicense(String realFileRelPath) throws Exception{ public ResponseEntity<byte[]> downloadDrivingLicense(String realFileRelPath) throws Exception{
String filePath = baseUploadPath + realFileRelPath; String filePath = baseUploadPath + realFileRelPath;
File file = new File(filePath);//新建一个文件 File file = new File(filePath);//新建一个文件
HttpHeaders headers = new HttpHeaders();//http头信息 HttpHeaders headers = new HttpHeaders();//http头信息
...@@ -152,7 +153,7 @@ public class VehicleBiz extends BaseBiz<VehicleMapper, Vehicle> { ...@@ -152,7 +153,7 @@ public class VehicleBiz extends BaseBiz<VehicleMapper, Vehicle> {
params.put("vehicle",vehicle); params.put("vehicle",vehicle);
params.put("yearMonth",yearMonth); params.put("yearMonth",yearMonth);
List<VehicleBookInfo> vehicleBookInfoList = vehicleBookInfoMapper.getByVehicleIdAndYearMonth(params); List<VehicleBookInfo> vehicleBookInfoList = vehicleBookInfoMapper.getByVehicleIdAndYearMonth(params);
return CollectionUtils.isEmpty(vehicleBookInfoList)?null:vehicleBookInfoList.get(0); return CollectionUtils.isEmpty(vehicleBookInfoList)? null:vehicleBookInfoList.get(0);
} }
/** /**
......
...@@ -156,6 +156,7 @@ public class VehicleModelController extends BaseController<VehicleModelBiz, Vehi ...@@ -156,6 +156,7 @@ public class VehicleModelController extends BaseController<VehicleModelBiz, Vehi
//设置信息 //设置信息
vm.setCrtName(uorr.getData().getName()); vm.setCrtName(uorr.getData().getName());
vm.setCrtUser(uorr.getData().getId()); vm.setCrtUser(uorr.getData().getId());
vm.setHotSign(2);
vm.setCrtTime(new Date()); vm.setCrtTime(new Date());
vm.setCrtHost(host); vm.setCrtHost(host);
vm.setIsdel(0); vm.setIsdel(0);
......
package com.xxfc.platform.vehicle; package com.xxfc.platform.vehicle;
import cn.hutool.core.date.DateUtil; import com.xxfc.platform.vehicle.entity.SimpleGenId;
import cn.hutool.core.io.file.FileWriter; import com.xxfc.platform.vehicle.entity.Student;
import cn.hutool.json.JSONUtil;
import com.github.wxiaoqi.security.common.msg.TableResultResponse;
import com.github.wxiaoqi.security.common.util.Query;
import com.xxfc.platform.vehicle.biz.SysRegionBiz;
import com.xxfc.platform.vehicle.entity.SysRegion;
import lombok.Data;
import org.joda.time.DateTime; import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter; import org.joda.time.format.DateTimeFormatter;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes={VehicleApplication.class}) @SpringBootTest(classes={VehicleApplication.class})
...@@ -48,22 +38,16 @@ public class TimeTest { ...@@ -48,22 +38,16 @@ public class TimeTest {
} }
@Test
public void testMapper(){
Student sOne = new Student("张4", "14",1);
sOne.getId();
// Assert.assertEquals();
SimpleGenId simpleGenId = new SimpleGenId();
Long aLong = simpleGenId.genId("6696", "122223");
} }
@Data
class Student implements Serializable {
private String name;
private String age;
private int code;
public Student(String name, String age, int code) {
this.name=name;
this.age=age;
this.code=code;
}
public Student(String name, String age) {
this.name=name;
this.age=age;
}
} }
package com.xxfc.platform.vehicle.entity;
public class IdService {
}
package com.xxfc.platform.vehicle.entity;
import tk.mybatis.mapper.genid.GenId;
public class SimpleGenId implements GenId<Long> {
private Long time;
private Integer seq;
@Override
public synchronized Long genId(String table, String column) {
long current = System.currentTimeMillis();
if (time == null || time != current) {
time = current;
seq = 1;
} else if (current == time) {
seq++;
}
return (time << 20) | seq;
}
}
\ No newline at end of file
package com.xxfc.platform.vehicle.entity;
import com.xxfc.platform.vehicle.entity.UUIdGenId;
import lombok.Data;
import tk.mybatis.mapper.annotation.KeySql;
import javax.persistence.Id;
import java.io.Serializable;
@Data
public class Student implements Serializable {
public static final Integer NO_AMOUNT_LIMIT = -1;
@Id
@KeySql(genId = UUIdGenId.class)
private Integer id;
private String name;
private String age;
private int code;
public Student(String name, String age, int code) {
this.name=name;
this.age=age;
this.code=code;
}
public Student(String name, String age) {
this.name=name;
this.age=age;
}
}
package com.xxfc.platform.vehicle.entity;
import tk.mybatis.mapper.genid.GenId;
import java.util.UUID;
public class UUIdGenId implements GenId {
@Override
public Object genId(String table, String column) {
return UUID.randomUUID().toString();
}
}
//package com.xxfc.platform.vehicle.entity;
//import tk.mybatis.mapper.genid.GenId;
//
//public class VestaGenId implements GenId<Long> {
// public Long genId(String table, String column){
// //ApplicationUtil.getBean 需要自己实现
// IdService idService = ApplicationUtil.getBean(IdService.class);
// return idService.genId();
// }
//}
\ 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