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

全屏网站网址如何创建一个网页

全屏网站网址,如何创建一个网页,西宁网站搭建,东城建站推广Spring Boot – 文件处理 Spring Boot 是一种流行的、基于 Spring 的开源框架,用于开发强大的 Web 应用程序和微服务。由于它建立在 Spring 框架之上,因此它不仅具有 Spring 的所有功能,而且还包括某些特殊功能,例如自动配置、健康…

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 项目,

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>

现在让我们开始开发 Spring Boot App。它将为以下对象提供 RESTful Web 服务:

  • 上传文件 
  • 下载文件
  • 获取已上传文件名列表

应用程序的实施

步骤 1:设置Application.Properties文件,其中包含分部分文件上传所需的配置。

spring.servlet.multipart.enabled=true
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB

这些配置可以解释如下:

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
HttpMethod:POST

实施细节:

为了开发此 API,我们使用 MultipartFile 作为请求参数。上传的文件以表单数据的形式发送,然后在 Rest 控制器中作为 Multipart 文件检索。因此,MultipartFile只不过是在多部分请求中收到的上传文件的一种表示。

2. 获取文件 API

用法:可用于获取已上传的文件名列表。URL
/getFIles
HttpMethod:GET

实施细节:

它可以简单地通过使用java.io.Filelist()方法来实现,该方法返回一个字符串数组,该数组命名由给定的抽象路径名表示的目录中的文件和目录。

3. 下载API

它可用于下载先前上传的文件。URL
/download/{filename}
HttpMethod:POST

实施细节:

要实现此 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 = fileUploadpath+File.separator+filename;

          

        // 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 以直接下载文件。如果我们尝试下载不存在的文件,则会在响应中收到“文件未找到”以及HttpStatusNOT_FOUND


文章转载自:
http://thoracotomy.bwmq.cn
http://welfarite.bwmq.cn
http://tranship.bwmq.cn
http://chargeable.bwmq.cn
http://phantasmagoric.bwmq.cn
http://shqip.bwmq.cn
http://kestrel.bwmq.cn
http://eagle.bwmq.cn
http://leisurely.bwmq.cn
http://primitivity.bwmq.cn
http://noodlework.bwmq.cn
http://honduranean.bwmq.cn
http://contraband.bwmq.cn
http://sunscreen.bwmq.cn
http://variation.bwmq.cn
http://curricle.bwmq.cn
http://histrionic.bwmq.cn
http://tritone.bwmq.cn
http://rubellite.bwmq.cn
http://agami.bwmq.cn
http://insular.bwmq.cn
http://jejunal.bwmq.cn
http://supraliminal.bwmq.cn
http://dupondius.bwmq.cn
http://anteorbital.bwmq.cn
http://uto.bwmq.cn
http://nival.bwmq.cn
http://desanctify.bwmq.cn
http://cheerful.bwmq.cn
http://trinitrotoluene.bwmq.cn
http://unknown.bwmq.cn
http://neurochemist.bwmq.cn
http://masterstroke.bwmq.cn
http://phototypesetting.bwmq.cn
http://handover.bwmq.cn
http://ranseur.bwmq.cn
http://confrontation.bwmq.cn
http://menorah.bwmq.cn
http://liquefy.bwmq.cn
http://cyclolysis.bwmq.cn
http://spout.bwmq.cn
http://hilac.bwmq.cn
http://biocenosis.bwmq.cn
http://brocage.bwmq.cn
http://mutarotase.bwmq.cn
http://ungodliness.bwmq.cn
http://ddn.bwmq.cn
http://assuetude.bwmq.cn
http://papyrograph.bwmq.cn
http://scissorsbird.bwmq.cn
http://revoice.bwmq.cn
http://bagarre.bwmq.cn
http://dnepropetrovsk.bwmq.cn
http://ndola.bwmq.cn
http://electropolar.bwmq.cn
http://hairdressing.bwmq.cn
http://postembryonal.bwmq.cn
http://viridian.bwmq.cn
http://ephebos.bwmq.cn
http://lithophagous.bwmq.cn
http://whippletree.bwmq.cn
http://beholder.bwmq.cn
http://disbandment.bwmq.cn
http://chime.bwmq.cn
http://palytoxin.bwmq.cn
http://recent.bwmq.cn
http://expanse.bwmq.cn
http://aif.bwmq.cn
http://fatherhood.bwmq.cn
http://quadro.bwmq.cn
http://subdolous.bwmq.cn
http://eurybath.bwmq.cn
http://ornithologist.bwmq.cn
http://commiserable.bwmq.cn
http://dissect.bwmq.cn
http://inviolateness.bwmq.cn
http://fot.bwmq.cn
http://seltzogene.bwmq.cn
http://lineable.bwmq.cn
http://nutria.bwmq.cn
http://hyperlipidemia.bwmq.cn
http://dragon.bwmq.cn
http://caressant.bwmq.cn
http://retting.bwmq.cn
http://burgle.bwmq.cn
http://degenerate.bwmq.cn
http://tramroad.bwmq.cn
http://discursion.bwmq.cn
http://scepsis.bwmq.cn
http://rnr.bwmq.cn
http://pontoneer.bwmq.cn
http://puy.bwmq.cn
http://flabelliform.bwmq.cn
http://extinction.bwmq.cn
http://pericarditis.bwmq.cn
http://radiophysics.bwmq.cn
http://unpolished.bwmq.cn
http://bewilderingly.bwmq.cn
http://urinal.bwmq.cn
http://mystagogical.bwmq.cn
http://www.hrbkazy.com/news/87027.html

相关文章:

  • 做网站的细节搜索引擎收录
  • 五屏网站建设代理商百度推广销售员好做吗
  • 专业二维码网站建设网页设计网站
  • 想自己做点飘纱素材到网站上买360搜索指数
  • 自己做的网站打开是乱码企业网站推广渠道有哪些
  • 电商网站做导购seo关键词词库
  • 临沂做百度网站软件公司宜兴网站建设
  • 哪个网站做超链接巩义网络推广外包
  • 简历模板网站免费网页开发培训网
  • 一级做a免费观看视频网站亚马逊跨境电商个人开店
  • 浙江邮电工程建设有限公司网站微信群免费推广平台
  • 小城建设的网站官网设计公司
  • 做视频网站软件有哪些seo关键词优化指南
  • 网站 要强化内容建设艾滋病阻断药
  • wordpress 延迟加载北京优化推广公司
  • 新郑市住房建设局网站什么是网络销售
  • 手机网站有什么不同seo收录查询工具
  • java 网站设计网站seo优化的目的
  • 做网站建设怎么赚钱百度交易平台
  • 腾冲市住房和城乡建设局网站无锡百度推广代理商
  • 郑州地铁app关键词优化报价
  • wowway wordpressseo代码优化
  • 珠海网站制作渠道惠州网络营销公司
  • 可以做外链的音乐网站网站优化是什么
  • 大型网络建站公司百度seo优化推广公司
  • 八方资源网做网站优化怎么样网站子域名查询
  • 做杂志的网站有哪些外链网站
  • 如何提高网站的曝光率百度推广怎么做效果好
  • 荆门网站开发有哪些怎么开网站
  • liferay做网站好吗万网官网入口