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

罗湖建网站公司江苏建设工程交易信息网站

罗湖建网站公司,江苏建设工程交易信息网站,科技成果转化,网站开发与设计实训Spring Boot – 文件处理 Spring Boot 是一种流行的、基于 Spring 的开源框架#xff0c;用于开发强大的 Web 应用程序和微服务。由于它建立在 Spring 框架之上#xff0c;因此它不仅具有 Spring 的所有功能#xff0c;而且还包括某些特殊功能#xff0c;例如自动配置、健康…Spring Boot – 文件处理 Spring Boot 是一种流行的、基于 Spring 的开源框架用于开发强大的 Web 应用程序和微服务。由于它建立在 Spring 框架之上因此它不仅具有 Spring 的所有功能而且还包括某些特殊功能例如自动配置、健康检查等。这使开发人员能够更轻松地以最少的配置设置基于 Spring 的应用程序从而促进快速应用程序开发。 Spring Boot 文件处理是指使用 RESTful Web 服务下载和上传文件。本文将逐步介绍如何使用 Spring Boot 实现可用于上传和下载文件的 RESTful Web 服务。 Spring Boot 中文件处理的初始设置 需要使用 Spring Initializer 创建具有Spring Web依赖项的 Spring Boot 项目 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency 现在让我们开始开发 Spring Boot App。它将为以下对象提供 RESTful Web 服务 上传文件 下载文件获取已上传文件名列表 应用程序的实施 步骤 1设置Application.Properties文件其中包含分部分文件上传所需的配置。 spring.servlet.multipart.enabledtrue spring.servlet.multipart.max-file-size10MB spring.servlet.multipart.max-request-size10MB这些配置可以解释如下 spring.servlet.multipart.enabled - 确定是否必须启用 multipart spring.servlet.multipart.max-file - 指定允许上传的文件的最大大小。spring.servlet.multipart.max-request-size - 指定允许的 multipart/form-data 请求的最大大小。 步骤 2创建一个 RestController FileController处理以下 REST API 1. 上传API 用法可用于上传文件。它使用多部分请求。URL /upload HttpMethodPOST 实施细节 为了开发此 API我们使用 MultipartFile 作为请求参数。上传的文件以表单数据的形式发送然后在 Rest 控制器中作为 Multipart 文件检索。因此MultipartFile只不过是在多部分请求中收到的上传文件的一种表示。 2. 获取文件 API 用法可用于获取已上传的文件名列表。URL /getFIles HttpMethodGET 实施细节 它可以简单地通过使用java.io.File的list()方法来实现该方法返回一个字符串数组该数组命名由给定的抽象路径名表示的目录中的文件和目录。 3. 下载API 它可用于下载先前上传的文件。URL /download/{filename} HttpMethodPOST 实施细节 要实现此 API我们首先检查所请求下载的文件是否存在于上传的文件夹中。如果文件存在我们使用InputStreamResource下载该文件。还需要将响应标头中的 Content-Disposition 设置为附件并将MediaType设置为application/octet-stream。  Content -Disposition响应头作为附件表示要下载内容。contentType设置为 application/octet-stream 这样当尝试下载缺少扩展名或格式未知的文件时系统会将其识别为八位字节流文件。FileController 的实现如下所示  Java // Java Program to Create Rest Controller  // that Defines various API for file handling package com.SpringBootFileHandling.controller;    // Importing required classes import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.util.Arrays;    import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.InputStreamResource; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile;    // Annotation RestController public class FileController {            // Uploading a file     RequestMapping(value /upload, method RequestMethod.POST)     public String uploadFile(RequestParam(file) MultipartFile file){            // Setting up the path of the file         String filePath System.getProperty(user.dir) /Uploads  File.separator file.getOriginalFilename();         String fileUploadStatus;                    // Try block to check exceptions         try {                            // Creating an object of FileOutputStream class               FileOutputStream fout new FileOutputStream(filePath);             fout.write(file.getBytes());                            // Closing the connection              fout.close();             fileUploadStatus File Uploaded Successfully;                        }                   // Catch block to handle exceptions         catch (Exception e) {             e.printStackTrace();             fileUploadStatus   Error in uploading file:   e;         }         return fileUploadStatus;     }            // Getting list of filenames that have been uploaded     RequestMapping(value /getFiles, method RequestMethod.GET)     public String[] getFiles()     {         String folderPath System.getProperty(user.dir) /Uploads;                      // Creating a new File instance         File directory new File(folderPath);                    // list() method returns an array of strings            // naming the files and directories            // in the directory denoted by this abstract pathname         String[] filenames directory.list();                    // returning the list of filenames         return filenames;                }            // Downloading a file     RequestMapping(value /download/{path:.}, method RequestMethod.GET)     public ResponseEntity downloadFile(PathVariable(path) String filename) throws FileNotFoundException {                // Checking whether the file requested for download exists or not         String fileUploadpath System.getProperty(user.dir) /Uploads;         String[] filenames this.getFiles();         boolean contains Arrays.asList(filenames).contains(filename);         if(!contains) {             return new ResponseEntity(FIle Not Found,HttpStatus.NOT_FOUND);         }                    // Setting up the filepath         String filePath fileUploadpathFile.separatorfilename;                    // Creating new file instance         File file new File(filePath);                    // Creating a new InputStreamResource object         InputStreamResource resource new InputStreamResource(new FileInputStream(file));                    // Creating a new instance of HttpHeaders Object         HttpHeaders headers new HttpHeaders();                    // Setting up values for contentType and headerValue         String contentType application/octet-stream;         String headerValue attachment; filename\  resource.getFilename() \;                         return ResponseEntity.ok()                 .contentType(MediaType.parseMediaType(contentType))                 .header(HttpHeaders.CONTENT_DISPOSITION, headerValue)                 .body(resource);                 } } 步骤3运行Spring Boot应用程序并使用postman测试API如下所示。 1. 上传API 为了上传文件我们需要在 Postman 中点击http://localhost:8080/upload并使用如下所示的表单数据 文件上传成功后我们可以在Uploads文件夹中看到该文件如下所示 2. 获取文件 API 我们需要在 postman 中点击http://localhost:8080/getFiles来获取已上传的文件名列表。 3. 下载API 为了下载文件我们需要在 postman 中点击http://localhost:8080/download/{filename}如下所示。 可以通过单击“保存响应”-“保存到文件”将 Postman 中收到的响应下载为文件。也可以在浏览器中点击下载 URL 以直接下载文件。如果我们尝试下载不存在的文件则会在响应中收到“文件未找到”以及HttpStatus为NOT_FOUND。
http://www.hkea.cn/news/14435737/

相关文章:

  • 手机网站制作机构更改wordpress主题
  • 定制 网站开发 价格帮建网站
  • 建设一个直播网站在线图片编辑器精简版
  • 免费设计网站免费网站服务器域名
  • vs开发网站开发教程昆明做凡科网站
  • 杭州做企业网站广告设计公司 上海
  • 深圳中高端网站建设怎么样可以做数据图的的网站有哪些
  • 广州网站搭建费用关键词权重如何打造
  • 免费手机建网站有哪些软件网络营销与直播电商专业就业前景
  • 成品网站源码在线本地网站开发公司
  • 湛江正规网站制作方案哪儿能做网站建设
  • 网站制作的流程公司网页维护
  • 山东住房和城乡建设局网站首页任务一 分析电子商务网站栏目结构
  • 南皮做网站价格百度指数只能查90天吗
  • 电子商务网站开发总结wordpress woo theme
  • 有必要自建网站做导购吗东莞营销型网站建站
  • 网站创建想法wordpress页脚内容居中
  • 个人网站号备案吗广州协安建设工程有限公司网站
  • 做网站的服务器有什么作用微信怎么创建自己的小程序
  • 中小学生在线做试卷的网站做网站市场价格多少钱
  • 在线制作网站系统修改wordpress后台登陆
  • 商务网站构建方法深圳外贸网站建设设计公司
  • 中山大良网站建设只做鞋子的网站
  • 网站后台数据改不了在线之家官网
  • 温州手机网站制作哪家便宜企业网站的主要内容
  • 网站建设的市场调研桂林漓江风景区
  • html建站做网站充值微信必须是企业
  • 九九电视剧免费观看完整版霞浦县网站seo优化排名
  • 做拍卖的网站极简 网站模板
  • 单位网站建设情况网站建设哪家好首推万维科技