<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
public class FileInfo {
private String path;
public FileInfo(String path) {
this.path = path;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}
3.1创建
@RestController
@RequestMapping("/file")
public class FileController {
/**
* 上传图片
* @param file
* @param req
* @return
* @throws Exception
*/
@PostMapping
public FileInfo upload(MultipartFile file,HttpServletRequest req) throws Exception {
System.out.Name());
System.out.OriginalFilename());//原图片名
System.out.Size());//大小
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
//图片名
String fileName = OriginalFilename(), "UTF-8");
fileName=sdf.format(new Date())+fileName.substring(fileName.lastIndexOf('.'));
//路径
String pathSession().getServletContext().getRealPath("/")+ "upload\";
File localFile = new File(path, fileName);
ansferTo(localFile);
//返回json格式
return new AbsolutePath());
}
/**
* 下载图片
* @param id
* @param request
* @param response
*/
@GetMapping("/{id}")//id为要下载的图片名:123.jsp
public void download(@PathVariable String id, HttpServletRequest request, HttpServletResponse response) {
String pathSession().getServletContext().getRealPath("/")+ "upload\";
//根据url获取输入流
//URL url = new URL(zipUrl);
//HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//设置超时间为3秒
//conn.setConnectTimeout(3*1000);
//防止屏蔽程序抓取而返回403错误
//conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
//得到输入流
//InputStream inputStream = InputStream();
//-------------------------------------------
//下载
try (InputStream inputStream = new FileInputStream(new File(path, id ));
OutputStream outputStream = OutputStream();) {
response.setContentType("application/x-download");
response.addHeader("Content-Disposition", "attachment;filename=" + id);
py(inputStream, outputStream);
} catch (Exception e) {
e.printStackTrace();
}
}
}
如果路径异常,用以下方法获取路径:
/**
* 获取图片上传路径
* @return
* @throws Exception
*/
public static String getPath() throws Exception {
//路径
File path = new URL("classpath:").getPath());
if (!ists()) {
path = new File("");
}
//如果上传目录为/static/images/upload/,则可以如下获取
File upload = new AbsolutePath(), "static/upload/");
if (!ists()) {
upload.mkdirs();
// System.out.AbsolutePath());
//在开发测试模式时,得到地址为:{项目跟目录}/target/static/images/upload/
//在打成jar正式发布时,得到的地址为:{发布jar包目录}/static/images/upload/
}
AbsolutePath();
}
3.2@RestController和@Controller区别
@RestController注解相当于@ResponseBody + @Controller合在一起的作用。
1) 如果只是使用@RestController注解Controller,则Controller中的方法无法返回jsp页面,或者html,配置的视图解析器 InternalResourceViewResolver不起作用,返回的内容就是Return 里的内容。
转json请看blog.csdn/qq_40369944/article/details/83898752
2) 如果需要返回到指定页面,则需要用 @Controller配合视图解析器InternalResourceViewResolver才行。
如果需要返回JSON,XML或自定义mediaType内容到页面,则需要在对应的方法上加上@ResponseBody注解。
4.1上传
4.1.1html:
<form action="/file" method="post" enctype="multipart/form-data" >
<input type="file" name="file">
<input type="submit" value="ok">
</form>
4.1.2页面:
4.1.3结果:
4.2下载
我就直接根据项目中的图片名下载
get方式 地址:
浏览器下载:
本文发布于:2025-02-23 23:22:00,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/1740324143579870.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |