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

制作网站公司网址赛雷猴是什么意思

制作网站公司网址,赛雷猴是什么意思,如何做属于自己的网站,网站设计大全目录 1、上传文件1.1、代码1.2、postman测试截图 2、下载resources目录中的模板文件2.1、项目结构2.2、代码2.3、使用场景 3、预览文件3.1、项目结构3.2、代码3.3、使用场景 1、上传文件 1.1、代码 PostMapping("/uploadFile") public String uploadFile(Multipart…

目录

      • 1、上传文件
        • 1.1、代码
        • 1.2、postman测试截图
      • 2、下载resources目录中的模板文件
        • 2.1、项目结构
        • 2.2、代码
        • 2.3、使用场景
      • 3、预览文件
        • 3.1、项目结构
        • 3.2、代码
        • 3.3、使用场景

1、上传文件

1.1、代码
@PostMapping("/uploadFile")
public String uploadFile(MultipartFile file) {System.out.println("文件名称:" + file.getOriginalFilename());return "成功";
}@PostMapping("/uploadFile2")
public String uploadFile2(@RequestParam("file") MultipartFile file
) {System.out.println("文件名称:" + file.getOriginalFilename());return "成功";
}@PostMapping("/uploadFile3")
public String uploadFile3(@RequestPart("file") MultipartFile file
) {System.out.println("文件名称:" + file.getOriginalFilename());return "成功";
}// 发送文件的同时带上参数
@PostMapping("/uploadFile4")
public String uploadFile4(@RequestPart("file") MultipartFile file, // 可以换成“MultipartFile file”或者“@RequestParam("file") MultipartFile file”@RequestParam("id") String id
) {System.out.println("文件名称:" + file.getOriginalFilename());System.out.println("id:" + id);return "成功";
}
1.2、postman测试截图

在这里插入图片描述

2、下载resources目录中的模板文件

2.1、项目结构

假设resources目录下有一个pdf文件:用户数据导入模板.xlsx,然后我们来下载该文件

在这里插入图片描述

2.2、代码
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.http.MediaTypeFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;@RestController
public class TestController {@GetMapping("/downloadLocalFile")public void downloadLocalFile(HttpServletResponse response) throws IOException {String fileName = "用户数据导入模板.xlsx";Resource r = new ClassPathResource(fileName);try (FileInputStream fileInputStream = new FileInputStream(r.getFile());ServletOutputStream outputStream = response.getOutputStream();) {response.setContentType("application/force-download");try {fileName = new String(fileName.getBytes("utf-8"), "ISO-8859-1");} catch (UnsupportedEncodingException e) {e.printStackTrace();}response.setHeader("Content-Disposition", "attachment;filename=" + fileName);IOUtils.copyLarge(fileInputStream, outputStream);} catch (Exception e) {e.printStackTrace();}}
}
2.3、使用场景

在这里插入图片描述

3、预览文件

3.1、项目结构

resources下面的文件为例,展示预览文件的代码,这是从本地获取文件,当然也可以通过其他方式获取文件

在这里插入图片描述

3.2、代码
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.http.MediaTypeFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;@RestController
public class TestController {@GetMapping("/previewLocalFile")public void previewLocalFile(HttpServletResponse response) throws IOException {String fileName = "SQL必知必会(第5版).pdf";Resource r = new ClassPathResource(fileName);try (FileInputStream fileInputStream = new FileInputStream(r.getFile());ServletOutputStream outputStream = response.getOutputStream();) {// 区别点1:将“response.setContentType("application/force-download");”替换成下面内容response.setContentType(MediaTypeFactory.getMediaType(fileName).orElse(MediaType.APPLICATION_OCTET_STREAM).toString());try {fileName = new String(fileName.getBytes("utf-8"), "ISO-8859-1");} catch (UnsupportedEncodingException e) {e.printStackTrace();}// 区别点2:预览是“filename”,下载是“attachment;filename=”response.setHeader("Content-Disposition", "filename=" + fileName);IOUtils.copyLarge(fileInputStream, outputStream);} catch (Exception e) {e.printStackTrace();}}
}
3.3、使用场景

在网盘软件中预览pdf文件

http://www.mmbaike.com/news/50463.html

相关文章:

  • 企业为什么要审计四年级下册数学优化设计答案
  • 南通制作企业网站营销推广软文案例
  • 专业做网站设计公司价格网络营销中心
  • 站酷海洛互联网营销怎么做
  • 无锡做公司网站百度推广优化工具
  • 米趋外贸网站建设外链交换平台
  • 做梯子的企业网站seo网站外链工具
  • 做欧美网站中铁建设集团有限公司
  • 建立主题网站的一般步骤西安今日头条最新新闻
  • 江西科技学校网站建设百seo排名优化
  • 可以做mv的视频网站品牌广告投放
  • 郑州网站推广地址真正免费的网站建站平台有哪些
  • 做网站的知名公司seo技术中心
  • 做网站一般需要哪些文件夹seo优化排名软件
  • 做外贸需要做网站吗山东今日头条新闻
  • 南昌做网站市场报价佛山百度seo点击软件
  • 凡科网做网站怎样阿里云网站搭建
  • 福州微信营销网站建设企业网站的搜索引擎推广与优化
  • 娱乐网站设计多少行业镇江推广公司
  • 还有什么网站可以做面包车拉货哈尔滨seo推广
  • 深圳视频制作公司微博seo营销
  • 做淘客网站要备案公司培训
  • 专门做微信公众号的网站vue seo优化
  • 国内最最早做虚拟货币的网站怎么弄一个自己的网站
  • 写一个wordpress下载插件seo是做什么工作内容
  • 台州网站公司seo课程培训
  • 怎么做短链接网站seo查询平台
  • 网站透明导航代码网络推广属于什么专业
  • 做的最好的手机网站seo网站优化多少钱
  • pc网站怎么做适配今日军事新闻报道