当前位置: 首页 > news >正文

target目录的文件上传controller,无需做静态映射。以及上传路径配置

target目录的文件上传controller

为什么说target?因为直接上传到target无需做静态资源映射,即可直接访问。而那些上传到别的目录下的,均需要做映射才能访问

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.UUID;@RestController
public class FileController {// 文件上传接口@RequestMapping("/upFile")public String upFile(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {// 设置文件保存路径String path = request.getServletContext().getRealPath("/upload");System.out.println("path:" + path);File realPath = new File(path);if (!realPath.exists()) {realPath.mkdir();}// 生成UUID作为文件名String uuid = UUID.randomUUID().toString();String originalFilename = file.getOriginalFilename();assert originalFilename != null;String newFilename = uuid + "_" + originalFilename;System.out.println("上传的文件地址:" + realPath);file.transferTo(new File(realPath + "/" + newFilename));return "localhost:8080/upload/" + newFilename;}// 文件下载接口@GetMapping("/download/{filename:.+}")public void downloadFile(@PathVariable("filename") String filename, HttpServletRequest request, HttpServletResponse response) throws IOException {System.out.println("Requested filename: " + filename);String path = request.getServletContext().getRealPath("/upload");File file = new File(path + "/" + filename);if (file.exists()) {response.setContentType("application/octet-stream");String encodedFilename = URLEncoder.encode(filename, "UTF-8").replaceAll("\\+", "%20");response.addHeader("Content-Disposition", "attachment; filename*=UTF-8''" + encodedFilename);byte[] buffer = new byte[1024];FileInputStream fis = null;BufferedInputStream bis = null;try {fis = new FileInputStream(file);bis = new BufferedInputStream(fis);OutputStream os = response.getOutputStream();int i;while ((i = bis.read(buffer)) != -1) {os.write(buffer, 0, i);}} catch (Exception e) {e.printStackTrace();} finally {if (bis != null) {bis.close();}if (fis != null) {fis.close();}}} else {response.sendError(HttpServletResponse.SC_NOT_FOUND, "File not found");}}
}

相关文章:

  • TQZC706开发板教程:10G光口ping测试
  • C++中的解释器模式
  • Java -jar 运行 报 MalformedInputException: Input length = 1
  • Mongodb数据库基本操作
  • jeecg快速启动(附带本地运行可用版本下载)
  • 可穿戴设备:苹果“吃老底”、华为“忙复苏”、小米“再扩容”
  • Java数据类型及运算符及数组(与C语言对比)
  • 数据治理:让数据提取更高效、更准确的关键
  • 综述:光学测量技术趋势
  • 辛弃疾,笔墨剑影的一生
  • UDP协议详解
  • Linux网络-HttpServer的实现
  • DP读书:半导体物理考试重点
  • 数据库中存储过程,看这一篇就够了!!
  • 从ES的JVM配置起步思考JVM常见参数优化
  • ES2017异步函数现已正式可用
  • Java 11 发布计划来了,已确定 3个 新特性!!
  • Javascript设计模式学习之Observer(观察者)模式
  • JavaWeb(学习笔记二)
  • JS数组方法汇总
  • PHP CLI应用的调试原理
  • php中curl和soap方式请求服务超时问题
  • Python 基础起步 (十) 什么叫函数?
  • vue从创建到完整的饿了么(11)组件的使用(svg图标及watch的简单使用)
  • 测试如何在敏捷团队中工作?
  • 回流、重绘及其优化
  • 数据科学 第 3 章 11 字符串处理
  • 一道闭包题引发的思考
  • 异步
  • 你对linux中grep命令知道多少?
  • Spark2.4.0源码分析之WorldCount 默认shuffling并行度为200(九) ...
  • ​软考-高级-信息系统项目管理师教程 第四版【第14章-项目沟通管理-思维导图】​
  • ​直流电和交流电有什么区别为什么这个时候又要变成直流电呢?交流转换到直流(整流器)直流变交流(逆变器)​
  • # 睡眠3秒_床上这样睡觉的人,睡眠质量多半不好
  • #pragma data_seg 共享数据区(转)
  • $con= MySQL有关填空题_2015年计算机二级考试《MySQL》提高练习题(10)
  • (DenseNet)Densely Connected Convolutional Networks--Gao Huang
  • (JSP)EL——优化登录界面,获取对象,获取数据
  • (办公)springboot配置aop处理请求.
  • (三)uboot源码分析
  • (转)shell调试方法
  • (转)shell中括号的特殊用法 linux if多条件判断
  • (转)大型网站架构演变和知识体系
  • .bat批处理(七):PC端从手机内复制文件到本地
  • .mkp勒索病毒解密方法|勒索病毒解决|勒索病毒恢复|数据库修复
  • .NET / MSBuild 扩展编译时什么时候用 BeforeTargets / AfterTargets 什么时候用 DependsOnTargets?
  • .net 桌面开发 运行一阵子就自动关闭_聊城旋转门家用价格大约是多少,全自动旋转门,期待合作...
  • .NET大文件上传知识整理
  • .net知识和学习方法系列(二十一)CLR-枚举
  • @RequestMapping 的作用是什么?
  • @transactional 方法执行完再commit_当@Transactional遇到@CacheEvict,你的代码是不是有bug!...
  • [ C++ ] 继承
  • [ACTF2020 新生赛]Include
  • [AutoSar]工程中的cpuload陷阱(三)测试
  • [Bug]使用gradio创建应用提示AttributeError: module ‘gradio‘ has no attribute ‘inputs‘