vue JAVA 下载文件,springboot整合vue实现上传下载文件

springboot整合vue实现上传下载文件,供大家参考,具体内容如下

环境

springboot 1.5.x

1、上传下载文件api文件

设置上传路径,如例子:

private final static String rootPath =

System.getProperty(“user.home”)+File.separator+fileDir+File.separator;

api接口:

下载url示例:http://localhost:8080/file/download?fileName=新建文本文档.txt

//上传不要用@Controller,用@RestController

@RestController

@RequestMapping("/file")

public class FileController {

private static final Logger logger = LoggerFactory.getLogger(FileController.class);

//在文件操作中,不用/或者\最好,推荐使用File.separator

private final static String fileDir="files";

private final static String rootPath = System.getProperty("user.home")+File.separator+fileDir+File.separator;

@RequestMapping("/upload")

public Object uploadFile(@RequestParam("file") MultipartFile[] multipartFiles, final HttpServletResponse response, final HttpServletRequest request){

File fileDir = new File(rootPath);

if (!fileDir.exists() && !fileDir.isDirectory()) {

fileDir.mkdirs();

}

try {

if (multipartFiles != null && multipartFiles.length > 0) {

for(int i = 0;i

try {

//以原来的名称命名,覆盖掉旧的

String storagePath = rootPath+multipartFiles[i].getOriginalFilename();

logger.info("上传的文件:" + multipartFiles[i].getName() + "," + multipartFiles[i].getContentType() + "," + multipartFiles[i].getOriginalFilename()

+",保存的路径为:" + storagePath);

Streams.copy(multipartFiles[i].getInputStream(), new FileOutputStream(storagePath), true);

//或者下面的

// Path path = Paths.get(storagePath);

//Files.write(path,multipartFiles[i].getBytes());

} catch (IOException e) {

logger.error(ExceptionUtils.getFullStackTrace(e));

}

}

}

} catch (Exception e) {

return ResultUtil.error(e.getMessage());

}

return ResultUtil.success("上传成功!");

}

/**

* http://localhost:8080/file/download?fileName=新建文本文档.txt

* @param fileName

* @param response

* @param request

* @return

*/

@RequestMapping("/download")

public Object downloadFile(@RequestParam String fileName, final HttpServletResponse response, final HttpServletRequest request){

OutputStream os = null;

InputStream is= null;

try {

// 取得输出流

os = response.getOutputStream();

// 清空输出流

response.reset();

response.setContentType("application/x-download;charset=GBK");

response.setHeader("Content-Disposition", "attachment;filename="+ new String(fileName.getBytes("utf-8"), "iso-8859-1"));

//读取流

File f = new File(rootPath+fileName);

is = new FileInputStream(f);

if (is == null) {

logger.error("下载附件失败,请检查文件“" + fileName + "”是否存在");

return ResultUtil.error("下载附件失败,请检查文件“" + fileName + "”是否存在");

}

//复制

IOUtils.copy(is, response.getOutputStream());

response.getOutputStream().flush();

} catch (IOException e) {

return ResultUtil.error("下载附件失败,error:"+e.getMessage());

}

//文件的关闭放在finally中

finally

{

try {

if (is != null) {

is.close();

}

} catch (IOException e) {

logger.error(ExceptionUtils.getFullStackTrace(e));

}

try {

if (os != null) {

os.close();

}

} catch (IOException e) {

logger.error(ExceptionUtils.getFullStackTrace(e));

}

}

return null;

}

}

访问:http://localhost:8080

a8dd7797db391b82717d8242cf9a3fb5.png

上传:

1a7eb8eaf0421e0abd9b0ed406b33af9.png

批量上传:

f5a6b5684c627733fe30f8595de35b48.png

下载:

dc192802373ea93b76794b640c0e2b81.png

2.上传大文件配置

/**

* 设置上传大文件大小,配置文件属性设置无效

*/

@Bean

public MultipartConfigElement multipartConfigElement() {

MultipartConfigFactory config = new MultipartConfigFactory();

config.setMaxFileSize("1100MB");

config.setMaxRequestSize("1100MB");

return config.createMultipartConfig();

}

3.vue前端主要部分

下载

点击上传

一次文件不超过1Gb

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值