分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

前后端分离,浏览器上传下载文件

发布时间:2023-09-19 22:10责任编辑:熊小新关键词:后端浏览器
服务器文件浏览器下载:
vue请求:

downloadService(){
?this.$confirm(‘确认要下载吗?‘, ‘提示‘, {
???confirmButtonText: ‘确认‘,
???cancelButtonText: ‘取消‘,
???type: ‘warning‘
?}).then(() => {
???window.open(encodeURI( restoreService.geturl()+
?????‘/asd/dasd?dsad))
???window.close()
?}).catch(() => {
???this.$message({
?????type: ‘info‘,
?????message: ‘已取消‘
???});
?})

},

//后端请求:

public ResponseEntity<byte[]> simpleExport() throws BackupsException {

try {

String filename = path+ name;
FileObject fileObject = new FileObject();
???fileObject = buildFileObject(fileObject, filename, name,contentsProj);
???return download(fileObject);

} catch (Exception e) {
e.printStackTrace();
msg = "Error:服务备份失败";
msgError = msg;
}

}

public static FileObject buildFileObject(FileObject fileObject, String fileName, String exportFileName,byte[] data) {
FileInputStream is = null;
FileOutputStream out = null;
try {
File file = new File(fileName);
if (file.exists()) {
file.delete();
}
File fileParent = file.getParentFile();
if(!fileParent.exists()){
fileParent.mkdirs();
}
out = new FileOutputStream(file);
out.write(data, 0, data.length);
is = new FileInputStream(new File(fileName));
fileObject.setStream(is);

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally{
if(out != null)
try {
out.close();
} catch (IOException e) {
}
}
fileObject.setId("2");
fileObject.setName(exportFileName);
fileObject.setSize(10000000);
fileObject.setType(".jar");
return fileObject;
}

public static ResponseEntity<byte[]> download(FileObject fo) throws UnsupportedEncodingException, IOException {
???????return download(fo.getName(), fo.getStream());
}

public static ResponseEntity<byte[]> download(String name, InputStream fis) throws UnsupportedEncodingException, IOException {
???????HttpHeaders headers = new HttpHeaders(); ?
// ???????String Agent = WebUtils.getRequest().getHeader("User-Agent");
???????/*if (null != Agent) { ?
???????????Agent = Agent.toLowerCase(); ?
???????????if (Agent.indexOf("Mozilla") != -1) { ?
???????????name = new String(name.getBytes(),"iso8859-1");
???????????} else { ?
???????????name = java.net.URLEncoder.encode(name,"UTF-8"); ?
???????????} ?
???????}*/
???????name = new String(name.replaceAll(" ", "").getBytes("gb2312"),"ISO_8859_1");
???????headers.setContentDispositionFormData("attachment", name); ???????
???????headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); ??
???????headers.set("X-fileName", name);
???????byte[] bytes = input2byte(fis);
???????return new ResponseEntity<byte[]>(bytes, headers, HttpStatus.OK); ???
}

???public static final byte[] input2byte(InputStream inStream) ?
???????????throws IOException { ?
???????ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); ?
???????byte[] buff = new byte[100]; ?
???????int rc = 0; ?
???????while ((rc = inStream.read(buff, 0, 100)) > 0) { ?
???????????swapStream.write(buff, 0, rc); ?
???????} ?
???????byte[] in2b = swapStream.toByteArray();
// ???????swapStream.close();
???????return in2b; ?
???}

浏览器上传文件到服务器:

vue请求:

uploadFile(params) {
?this.loading = true
?const _file = params.file;
?// const isLt2M = _file.size / 1024 / 1024 < 2;
?// 通过 FormData 对象上传文件
?var formData = new FormData();
?formData.append("file", _file);
?// if (!isLt2M) {
?// ??this.$message.error("请上传2M以下的.xlsx文件");
?// ??return false;
?// }
?this.$axios.post(restoreService.geturl() +‘/asd/sdf?we‘,formData,{ headers : { ‘Content-type‘:‘multipart/form-data‘}}).then(res => {
???if (res.data.status === ‘200‘) {
???????????????this.$message({
?????????????????type: ‘success‘,
?????????????????message: res.data.message,
???????????????})
?????????????} else {
???????????????this.$message({
?????????????????type: ‘warning‘,
?????????????????message: res.data.message
???????????????})
?????????????}
???this.$refs.upload.clearFiles();
???this.loading = false
???????????})
?????},

//后端请求:

@RequestMapping(value = "/Import", method = RequestMethod.POST)
public BaseResult<BackUpDto> simpleImport(@RequestParam(value = "file") MultipartFile file) throws Exception {
???return logBackupRestoreService.simpleImport(file.getInputStream());
}

public BaseResult<BackUpDto> simpleImport(InputStream inputStream) throws BackupsException {
String path = System.getProperty("user.dir");
String fileGuid = UUID.randomUUID().toString();
File file = new File(path + "\\" + fileGuid + ".tmp");
byte[] bytes = null;
try {
bytes = inputStreamToFile(inputStream, file);
} catch (Exception e) {

}

}

public static byte[] inputStreamToFile(InputStream ins, File file) throws Exception{
ByteArrayOutputStream ous = null;
//OutputStream os = null;
try {
//os = new FileOutputStream(file);
ous = new ByteArrayOutputStream();
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
ous.write(buffer, 0, bytesRead);
}
} catch (Exception e) {
//logger.error(e.getMessage(), e);
throw new Exception(e);
}finally{
if(ous != null){
ous.close();
}
if(ins != null){
ins.close();
}

}
return ous.toByteArray();
}



前后端分离,浏览器上传下载文件

原文地址:https://www.cnblogs.com/js1314/p/10789021.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved