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

广州公司注册代理公司注册服务长岭网站优化公司

广州公司注册代理公司注册服务,长岭网站优化公司,《营销型网站建设实战》,团购网站自个做目录 1. 新建一个springboot项目2. 配置文件application.propertiesapplication.yml 3. 控制类实现文件上传和下载4. 测试 1. 新建一个springboot项目 新建一个springboot项目&#xff0c;选择web&#xff0c;默认即可. 主要pom配置文件如下&#xff1a; <parent><gr…

目录

  • 1. 新建一个springboot项目
  • 2. 配置文件
    • application.properties
    • application.yml
  • 3. 控制类实现文件上传和下载
  • 4. 测试

1. 新建一个springboot项目

      新建一个springboot项目,选择web,默认即可.

      主要pom配置文件如下:

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.10.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><maven.compiler.source>11</maven.compiler.source><maven.compiler.target>11</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><!--web包--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies>
<!--    maven打包设置--><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><includeSystemScope>true</includeSystemScope></configuration><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins></build>

2. 配置文件

      在配置文件中配置文件上传和下载的根目录,文件大小限制等。

application.properties

file.upload.url=path/to/upload
# 文件大小限制
spring.servlet.multipart.max-file-size=10MB 
# 请求大小限制
spring.servlet.multipart.max-request-size=20MB

application.yml

file.upload.url: path/to/upload
spring:servlet:multipart:# 文件大小限制max-file-size: 10MBmax-request-size: 20MB

3. 控制类实现文件上传和下载

      在控制类中实现文件上传和下载。其中,文件上传通过MultipartFile实现文件类型限制、文件大小限制、多文件上传等,下载通过文件流实现。

@RestController
@RequestMapping(value = "/file")
@CrossOrigin // 允许跨域
@ControllerAdvice // 全局异常处理
public class FileController {//上传的根目录@Value("${file.upload.url}")private String uploadFilePath;@Value("${file.upload.url}")private String downloadFilePath;//捕获上传文件过大的异常//@ExceptionHandler标识方法将处理指定类型的异常@ExceptionHandler(MaxUploadSizeExceededException.class)public ResponseEntity<String> handleMaxSizeException(MaxUploadSizeExceededException exc) {return ResponseEntity.status(HttpStatus.PAYLOAD_TOO_LARGE).body("文件过大,请重新选择上传图片!");}@RequestMapping("/upload")public String httpUpload(@RequestParam("files")//MultipartFile 是 Spring 框架中用于处理文件上传的接口MultipartFile files[]) {StringBuffer urls = new StringBuffer();//遍历多个文件for (int i = 0; i < files.length; i++) {//获取文件类型String contentType = files[i].getContentType();System.out.println(contentType);if (!contentType.contains("image")) {return "文件格式不正确,请重新选择图片!";}//获取上传文件的后缀名String postfix = files[i].getOriginalFilename().substring(files[i].getOriginalFilename().lastIndexOf("."));//生成文件名 使用时间戳,避免重名String fileName = System.currentTimeMillis()+postfix;System.out.println(fileName);
//            File dest = new File(uploadFilePath + '/' + fileName);File dest = new File(uploadFilePath + '/' + fileName);//目录不存在则创建目录if (!dest.getParentFile().exists()) {dest.getParentFile().mkdirs();}try {//将上传的文件保存到指定的目标位置files[i].transferTo(dest);} catch (Exception e) {return "程序错误,请重新上传\n" + e.toString();}urls.append("\n"+uploadFilePath + '\\' + fileName);}return "文件上传成功" + urls.toString();}@RequestMapping("/download")public String fileDownLoad(HttpServletResponse response, @RequestParam("fileName") String fileName) {
//        File file = new File(downloadFilePath + '/' + fileName);File file = new File(downloadFilePath + '\\' + fileName);if (!file.exists()) {return "下载文件不存在";}//清除之前设置的内容response.reset();//设置响应内容类型为二进制流response.setContentType("application/octet-stream");//设置响应字符编码为UTF-8response.setCharacterEncoding("utf-8");//设置响应文件大小response.setContentLength((int) file.length());//设置浏览器以附件的形式下载,并设置文件名response.setHeader("Content-Disposition", "attachment;filename=" + fileName);try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));) {byte[] buff = new byte[1024];OutputStream os = response.getOutputStream();int i = 0;while ((i = bis.read(buff)) != -1) {os.write(buff, 0, i);os.flush();}} catch (IOException e) {return "下载失败";}return "下载成功";}
}

4. 测试

      以下测试使用工具postman实现。测试文件大小的图片可以使用代码生成,可参考文章。

在这里插入图片描述在这里插入图片描述
在这里插入图片描述


参考链接:

https://cloud.tencent.com/developer/article/1594124

https://cloud.baidu.com/article/2773207

https://blog.csdn.net/m0_63297090?type=blog

http://www.hkea.cn/news/683604/

相关文章:

  • 网页设计要用到什么软件聊城seo优化
  • 用wordpress做网站百度推广管理
  • 一个空间可以放两个网站吗html模板网站
  • 做试用网站的原理网站推广优化平台
  • 软件工程培训机构学费亚马逊seo什么意思
  • 做恶搞网站软件有哪些苏州seo怎么做
  • 怎么做微信小说网站企业网络营销策划方案
  • 网站后台上传图片失败百度下载免费安装最新版
  • 镇江做网站需要多少钱企业网站模板设计
  • 西安seo优化系统网页seo
  • 如何用网站模板做网站广州网络营销推广
  • 承德手机网站建设seo推广排名
  • wordpress块引用一个网站可以优化多少关键词
  • 360网站卖东西怎么做的无锡seo优化公司
  • 邢台人民网站百度视频推广怎么收费
  • 常州天启建设公司网站高端快速建站
  • ppt模板免费下载网站不用登录seo测试工具
  • 四川建设人才网官网查询阜新网站seo
  • 太原网站开发定制百度网盘官网下载
  • 业主装修日记那个网站做的好片多多可以免费看电视剧吗
  • 租车网站建设站长之家源码
  • 昌吉州回族自治州建设局网站地产渠道12种拓客方式
  • 北京市网站公司网络项目免费的资源网
  • 电子商务网站规划、电子商务网站建设站长工具 忘忧草
  • 凡科建网关键词优化公司哪家好
  • seo排名推广工具seo公司多少钱
  • 做视频网站赚钱怎么在百度上推广自己的公司信息
  • 网站建设凡科厦门网站建设平台
  • 互联网行业pest分析福州百度快速优化排名
  • 做网站的接私活犯法吗如何对网站进行推广