Commit b54fe7f7 authored by 耿迪迪's avatar 耿迪迪

图片下载-上传问题修改

parent bbaef679
package com.zehong.web.controller.transaction;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zehong.common.annotation.Log;
import com.zehong.common.core.controller.BaseController;
import com.zehong.common.core.domain.AjaxResult;
import com.zehong.common.core.exception.BusinessException;
import com.zehong.common.core.page.TableDataInfo;
import com.zehong.common.enums.BusinessType;
import com.zehong.common.utils.StringUtils;
import com.zehong.common.utils.poi.ExcelUtil;
import com.zehong.system.domain.TTradeProject;
import com.zehong.system.service.ITTradeProjectService;
......@@ -12,7 +16,18 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* 交易项目Controller
......@@ -133,4 +148,98 @@ public class TTradeProjectController extends BaseController
public AjaxResult settlePendingPayment(@RequestBody TTradeProject tTradeProject) {
return toAjax(tTradeProjectService.settlePendingPayment(tTradeProject));
}
@GetMapping("/batchDownload/{tradeId}")
public void batchDownload(@PathVariable("tradeId") Long tradeId, HttpServletResponse response){
try {
TTradeProject tradeProject = tTradeProjectService.selectTTradeProjectById(tradeId);
String attachment = tradeProject.getAttachmentUrl();
List<JSONObject> attachmentJson = JSON.parseArray(attachment,JSONObject.class);
// 清空response
response.reset();
// 设置response的Header
//response.setCharacterEncoding("UTF-8");
//Content-Disposition的作用:告知浏览器以何种方式显示响应返回的文件,用浏览器打开还是以附件的形式下载到本地保存
//attachment表示以附件方式下载 inline表示在线打开 "Content-Disposition: inline; filename=文件名.mp3"
// filename表示文件的默认名称,因为网络传输只支持URL编码的相关支付,因此需要将文件名URL编码后进行传输,前端收到后需要反编码才能获取到真正的名称
// 它会同时设置服务器和客户端都使用 UTF-8 字符集,还设置了响应头
// 此方法一定要在获取流对象之前调用才有效
response.setContentType("text/html; charset=UTF-8");
//设置响应格式,已文件流的方式返回给前端。
response.setContentType("application/octet-stream");
// 告知浏览器文件的大小
OutputStream outputStream = response.getOutputStream();
if(attachmentJson.size()>1){
//打包下载
toZip(response,attachmentJson);
return;
}
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(getFileName(attachmentJson.get(0)), "UTF-8"));
URLConnection file = new URL(getFileUrl(attachmentJson.get(0).getString("url"))).openConnection();
InputStream is = file.getInputStream();
// 1K的数据缓冲
byte[] bs = new byte[1024];
// 读取到的数据长度
int len;
// 输出的文件流
while ((len = is.read(bs)) != -1) {
outputStream.write(bs, 0, len);
}
// 完毕,关闭所有链接
outputStream.close();
is.close();
outputStream.flush();
} catch (Exception e) {
logger.error("附件下载失败:" + e);
throw new BusinessException("附件下载失败!");
}
}
/**
* 大压缩包下载
* @param response 返回
* @param attachmentJson 附件信息
* @throws IOException
*/
private void toZip(HttpServletResponse response,List<JSONObject> attachmentJson) throws IOException {
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("附件-"+ new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".zip", "UTF-8"));
ZipOutputStream zipOutputStream = new ZipOutputStream(response.getOutputStream());
for(JSONObject am : attachmentJson){
URLConnection file = new URL(getFileUrl(am.getString("url"))).openConnection();
InputStream is = file.getInputStream();
//设置压缩包内文件的名称
zipOutputStream.putNextEntry(new ZipEntry(getFileName(am)));
int size;
byte[] buffer = new byte[4096];
while ((size = is.read(buffer)) > 0) {
zipOutputStream.write(buffer, 0, size);
}
zipOutputStream.closeEntry();
is.close();
}
zipOutputStream.close();
zipOutputStream.flush();
}
/**
* 获取文件名称
* @param fileInfo 附件信息
* @return
*/
private String getFileName(JSONObject fileInfo){
String name = fileInfo.getString("name");
if(StringUtils.isNotEmpty(name)){
return name;
}
String[] urlStr = fileInfo.getString("url").split("/");
return urlStr[urlStr.length - 1];
}
private String getFileUrl(String url){
return url.contains("36.139.131.221:8904") ? url.replace("36.139.131.221:8904","127.0.0.1:8668") : url;
}
}
......@@ -97,7 +97,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter
// 过滤请求
.authorizeRequests()
// 对于登录login 验证码captchaImage 允许匿名访问
.antMatchers("/login","/changeLogin", "/captchaImage", "/webSocket/**").anonymous()
.antMatchers("/login","/changeLogin", "/captchaImage", "/webSocket/**","/trade/project/batchDownload/**").anonymous()
.antMatchers(
HttpMethod.GET,
"/*.html",
......
......@@ -9,3 +9,6 @@ VUE_CLI_BABEL_TRANSPILE_MODULES = true
# websocket地址
VUE_APP_WEBSOCKET_URL = 'ws://localhost:8668/precisionEffect/webSocket/'
#download地址
VUE_APP_DOWNLOAD_URL = 'http://localhost:8668/precisionEffect'
......@@ -6,3 +6,6 @@ VUE_APP_BASE_API = '/prod-api'
# websocket地址
VUE_APP_WEBSOCKET_URL = 'ws://36.139.131.221:8904/precisionEffect/webSocket/'
#download地址
VUE_APP_DOWNLOAD_URL = 'http://36.139.131.221:8904/precisionEffect'
......@@ -158,7 +158,8 @@
})
}
this.$message.success("上传成功");
this.$emit('resFun', this.fileList)
this.$emit('resFun', this.fileList);
this.fileList = [];
} else {
fileList.filter(o => o.uid != file.uid)
this.$emit('resFun', this.fileList);
......@@ -168,6 +169,10 @@
// 文件列表移除文件
handleRemove(file, fileList) {
console.log("列表移除", file, fileList);
/* let index = this.fileList.findIndex(item => (item.name?item.name:item.url.split("/")[item.url.split("/").length -1]) == file.name);
if(index != -1){
this.fileList.splice(index,1);
}*/
this.addShow = fileList.length > 0 ? true : false;
this.$emit("remove", file);
},
......
......@@ -57,7 +57,7 @@
<span
style="padding-top: 10px;"
class="dbtn"
@click="download(tradeData.attachmentUrl)"
@click="download(tradeData.tradeId)"
v-if="tradeData.attachmentUrl != null && tradeData.attachmentUrl!=''"
>
<i class="el-icon el-icon-view"></i>查看/下载
......@@ -113,6 +113,7 @@
<script>
import { getProject } from "@/api/transaction/project";
import { batchDownload } from "@/api/transaction/tradeProject"
export default {
name: "common-info",
props:{
......@@ -140,9 +141,9 @@
window.open(url,'_blank');
},
//附件下载
download(url) {
download(tradeId) {
// url = url.replace(/\\/g, "/");
let urls = JSON.parse(url);
/* let urls = JSON.parse(url);
urls.forEach(item =>{
const xhr = new XMLHttpRequest();
xhr.open("GET", item.url, true);
......@@ -179,7 +180,9 @@
};
xhr.send();
})
})*/
//batchDownload("12323");
window.open(process.env.VUE_APP_DOWNLOAD_URL + "/trade/project/batchDownload/" + tradeId);
},
......
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