Commit bf12a8da authored by 赵啸非's avatar 赵啸非

添加巴中经开区windows shell

parent a1005789
package com.mortals.xhx.base.system.upload.web;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.annotation.UnAuth;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.web.BaseController;
import com.mortals.xhx.base.system.upload.service.UploadService;
import com.mortals.xhx.module.device.model.DeviceModuleDistributeEntity;
import com.mortals.xhx.module.device.model.DeviceModuleDistributeQuery;
import com.mortals.xhx.module.device.service.DeviceModuleDistributeService;
import com.mortals.xhx.module.product.model.ProductVersionEntity;
import com.mortals.xhx.module.product.service.ProductVersionService;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* 上传文件
......@@ -27,6 +42,11 @@ public class UploadController extends BaseController {
@Resource
private UploadService uploadService;
@Autowired
private ProductVersionService productVersionService;
@Autowired
private DeviceModuleDistributeService deviceModuleDistributeService;
@RequestMapping(value = "upload")
@UnAuth
......@@ -55,7 +75,7 @@ public class UploadController extends BaseController {
@RequestMapping(value = "commonupload")
@UnAuth
public String doFileUpload(MultipartFile file, @RequestParam(value = "prePath",defaultValue = "file/fileupload") String prePath) {
public String doFileUpload(MultipartFile file, @RequestParam(value = "prePath", defaultValue = "file/fileupload") String prePath) {
Map<String, Object> model = new HashMap<>();
String jsonStr = "";
try {
......@@ -100,7 +120,7 @@ public class UploadController extends BaseController {
* @param fileName 文件名称
*/
@GetMapping("preview/{fileName}")
public void preView(@PathVariable(value="fileName") String fileName, HttpServletResponse response) {
public void preView(@PathVariable(value = "fileName") String fileName, HttpServletResponse response) {
try {
uploadService.preview(fileName, response);
} catch (Exception e) {
......@@ -114,7 +134,7 @@ public class UploadController extends BaseController {
* @param fileName 文件名称
*/
@GetMapping("fileupload/{fileName}")
public void fileupload(@PathVariable(value="fileName") String fileName, HttpServletResponse response) {
public void fileupload(@PathVariable(value = "fileName") String fileName, HttpServletResponse response) {
try {
uploadService.uploadDownload(fileName, response);
} catch (Exception e) {
......@@ -123,4 +143,74 @@ public class UploadController extends BaseController {
}
/**
* 获取所有相关资源文件并压缩打包成zip
*/
@GetMapping(value = "zip")
@UnAuth
public void zip() {
JSONObject jsonObject = new JSONObject();
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ZipOutputStream zip = new ZipOutputStream(outputStream);
List<ProductVersionEntity> productVersionEntities = productVersionService.find(new ProductVersionEntity());
for (ProductVersionEntity productVersionEntity : productVersionEntities) {
String filePath = productVersionEntity.getFilePath();
filePath = uploadService.getFilePath(filePath);
File file = new File(filePath);
if (file.exists()) {
try {
zip.putNextEntry(new ZipEntry(StrUtil.removePrefix(filePath, "/")));
IOUtils.write(FileUtil.readBytes(file), zip);
zip.flush();
zip.closeEntry();
} catch (Exception e) {
log.error("异常", e);
}
}
}
List<DeviceModuleDistributeEntity> deviceModuleDistributeEntities = deviceModuleDistributeService.find(new DeviceModuleDistributeQuery().siteId(1L));
for (DeviceModuleDistributeEntity deviceModuleDistributeEntity : deviceModuleDistributeEntities) {
String filePath = deviceModuleDistributeEntity.getFilePath();
filePath = uploadService.getFilePath(filePath);
File file = new File(filePath);
if (file.exists()) {
try {
zip.putNextEntry(new ZipEntry(StrUtil.removePrefix(filePath, "/")));
IOUtils.write(FileUtil.readBytes(file), zip);
zip.flush();
zip.closeEntry();
} catch (Exception e) {
log.error("异常", e);
}
}
}
IOUtils.closeQuietly(zip);
byte[] bytes = outputStream.toByteArray();
genCode(response, bytes);
jsonObject.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
jsonObject.put(KEY_RESULT_MSG, "压缩文件成功!");
} catch (Exception e) {
log.error("获取异常", e);
jsonObject.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
}
}
/**
* 生成zip文件
*/
private void genCode(HttpServletResponse response, byte[] data) throws IOException {
response.reset();
response.addHeader("Access-Control-Allow-Origin", "*");
response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
response.setHeader("Content-Disposition", "attachment; filename=\"skin.zip\"");
response.addHeader("Content-Length", "" + data.length);
response.setContentType("application/octet-stream; charset=UTF-8");
IOUtils.write(data, response.getOutputStream());
}
}
\ 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