当前位置: 首页 > 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.shuangfujiaoyu.com/news/27366.html

相关文章:

  • 做企业网站收费多少建立一个网站需要多少钱
  • 做数学题挣钱的网站it培训机构哪家好
  • 电商网站建设运营协议百度手机助手app免费下载
  • 手机wap网站cms源码龙华线上推广
  • 模板网站购买北京最新发布信息
  • 汉南做网站个人免费网上注册公司
  • 编程培训机构哪个靠谱西安关键词seo
  • 学做课件的网站网络媒体广告代理
  • 互联网营销网站建设泉州全网营销推广
  • dw 个人网站怎么做优化排名案例
  • 潜江网站建设b2b网站源码
  • 怎么上传网站源码2022最新国际新闻10条简短
  • 云南省建设厅官网什么叫seo优化
  • 政府网站建设集约化服务器h5制作
  • p2p网站建设应注意的问题厦门seo优化
  • 微信小程序怎么制作商城关键词优化设计
  • 自己做网站 微信杭州关键词优化测试
  • 新手想做网站赚钱优化营商环境心得体会1000字
  • 手机商城网站开发怎么进行网站推广
  • 招聘网站建设方案模板下载十大免费软文推广平台
  • 自己的域名可以转给做网站的账号吗外贸网站推广公司
  • 东莞专业网站营销微博营销软件
  • 重庆企业网站建设亚马逊的免费网站
  • 建设校园网站wbs计划营销策划案
  • 三级分销平台天津做优化好的公司
  • 不需要证件做网站如何制作一个个人网站
  • 网页制作基础教程第二版seo网站关键词排名提升
  • 购物商城网站制作郑州纯手工seo
  • 网站模板对seo的影响吗百度站长号购买
  • 焦作专业做网站公司百度框架户一级代理商