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

中文.com网站行业关键词查询

中文.com网站,行业关键词查询,做一个网站要怎么做,我的世界做指令的网站在Spring Boot项目中实现文件的上传、下载和预览功能,可以通过使用Spring MVC的MultipartFile接口来处理文件上传,并使用HttpServletResponse或Resource来实现文件下载和预览。下面是如何实现这些功能的完整示例。 1. 引入依赖 确保在pom.xml中引入了S…

在Spring Boot项目中实现文件的上传、下载和预览功能,可以通过使用Spring MVC的MultipartFile接口来处理文件上传,并使用HttpServletResponseResource来实现文件下载和预览。下面是如何实现这些功能的完整示例。

1. 引入依赖

确保在pom.xml中引入了Spring Boot的相关依赖。通常情况下,Spring Boot Starter Web已经包含了必要的依赖。

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

2. 创建文件上传、下载和预览的Controller

你可以创建一个FileController来处理文件的上传、下载和预览。

import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;@Controller
@RequestMapping("/files")
public class FileController {// 文件保存路径(可以通过配置文件进行配置)@Value("${file.upload-dir}")private String fileUploadDir;// 文件上传@PostMapping("/upload")@ResponseBodypublic String uploadFile(@RequestParam("file") MultipartFile file) throws IOException {// 获取文件名并且保存文件String fileName = file.getOriginalFilename();Path targetLocation = Paths.get(fileUploadDir).resolve(fileName);Files.copy(file.getInputStream(), targetLocation, StandardCopyOption.REPLACE_EXISTING);// 返回文件下载的URLString fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath().path("/files/download/").path(fileName).toUriString();return "File uploaded successfully: " + fileDownloadUri;}// 文件下载@GetMapping("/download/{fileName}")public ResponseEntity<Resource> downloadFile(@PathVariable String fileName) throws MalformedURLException {Path filePath = Paths.get(fileUploadDir).resolve(fileName).normalize();Resource resource = new UrlResource(filePath.toUri());if (!resource.exists()) {return ResponseEntity.notFound().build();}return ResponseEntity.ok().contentType(MediaType.APPLICATION_OCTET_STREAM).header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"").body(resource);}// 文件预览(主要针对图片、PDF等可以直接在浏览器中显示的文件)@GetMapping("/preview/{fileName}")public ResponseEntity<Resource> previewFile(@PathVariable String fileName) throws MalformedURLException {Path filePath = Paths.get(fileUploadDir).resolve(fileName).normalize();Resource resource = new UrlResource(filePath.toUri());if (!resource.exists()) {return ResponseEntity.notFound().build();}String contentType = "application/octet-stream";try {contentType = Files.probeContentType(filePath);} catch (IOException e) {e.printStackTrace();}return ResponseEntity.ok().contentType(MediaType.parseMediaType(contentType)).body(resource);}
}

3. 配置文件上传目录

application.propertiesapplication.yml中配置文件的上传路径:

file.upload-dir=C:/uploads

或者使用application.yml

file:upload-dir: C:/uploads

你可以将路径配置为你项目的目录,也可以指定到服务器的某个位置。

4. 创建上传目录

确保在你的系统上已经创建了配置文件中指定的上传目录,比如C:/uploads。如果没有创建,可以通过代码在项目启动时自动创建:

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;@Component
public class FileUploadDirectoryInitializer implements CommandLineRunner {@Value("${file.upload-dir}")private String fileUploadDir;@Overridepublic void run(String... args) throws Exception {Path uploadPath = Paths.get(fileUploadDir);if (!Files.exists(uploadPath)) {Files.createDirectories(uploadPath);}}
}

5. 测试上传、下载和预览

5.1 文件上传

你可以使用Postman、cURL或者前端页面来测试文件上传功能。上传文件的URL为:

POST http://localhost:8080/files/upload

参数名称为file

5.2 文件下载

你可以通过以下URL下载文件:

GET http://localhost:8080/files/download/{fileName}

其中{fileName}是文件的名称。

5.3 文件预览

你可以通过以下URL预览文件(如图片或PDF):

GET http://localhost:8080/files/preview/{fileName}

6. 处理大文件上传

对于大文件上传,Spring Boot默认的最大上传文件大小可能不满足需求,可以通过以下配置进行调整:

spring.servlet.multipart.max-file-size=100MB
spring.servlet.multipart.max-request-size=100MB

或者在application.yml中:

spring:servlet:multipart:max-file-size: 100MBmax-request-size: 100MB

7. 文件预览类型支持

通常情况下,浏览器支持预览的文件类型包括图片(如jpegpng)、PDF、文本文件等。如果文件类型不被浏览器支持,通常可以通过文件下载的方式处理。

8. 文件删除功能(可选)

你也可以添加一个删除文件的接口,来删除已上传的文件:

// 文件删除
@DeleteMapping("/delete/{fileName}")
@ResponseBody
public String deleteFile(@PathVariable String fileName) throws IOException {Path filePath = Paths.get(fileUploadDir).resolve(fileName).normalize();Files.deleteIfExists(filePath);return "File deleted successfully";
}

文章转载自:
http://semble.bbmx.cn
http://vomitous.bbmx.cn
http://undose.bbmx.cn
http://greyly.bbmx.cn
http://upholsterer.bbmx.cn
http://sleepcoat.bbmx.cn
http://authoritarianism.bbmx.cn
http://hairsplitter.bbmx.cn
http://licit.bbmx.cn
http://matelote.bbmx.cn
http://antechamber.bbmx.cn
http://mauger.bbmx.cn
http://invidious.bbmx.cn
http://absorbing.bbmx.cn
http://girly.bbmx.cn
http://injuriously.bbmx.cn
http://imponderability.bbmx.cn
http://teahouse.bbmx.cn
http://ddr.bbmx.cn
http://carotin.bbmx.cn
http://nacred.bbmx.cn
http://donnard.bbmx.cn
http://coziness.bbmx.cn
http://barstool.bbmx.cn
http://transphasor.bbmx.cn
http://sough.bbmx.cn
http://istana.bbmx.cn
http://headstone.bbmx.cn
http://setover.bbmx.cn
http://okazaki.bbmx.cn
http://etu.bbmx.cn
http://hodgepodge.bbmx.cn
http://reencounter.bbmx.cn
http://sempervivum.bbmx.cn
http://ungrudgingly.bbmx.cn
http://excitron.bbmx.cn
http://evict.bbmx.cn
http://norbert.bbmx.cn
http://slugging.bbmx.cn
http://most.bbmx.cn
http://rhizocarpous.bbmx.cn
http://whiny.bbmx.cn
http://wastemaster.bbmx.cn
http://loco.bbmx.cn
http://frutex.bbmx.cn
http://polymnia.bbmx.cn
http://morra.bbmx.cn
http://contredanse.bbmx.cn
http://fractional.bbmx.cn
http://yankeeism.bbmx.cn
http://idyllist.bbmx.cn
http://renew.bbmx.cn
http://ctenidium.bbmx.cn
http://fantastically.bbmx.cn
http://irresolute.bbmx.cn
http://tincture.bbmx.cn
http://opportunity.bbmx.cn
http://frills.bbmx.cn
http://linkboy.bbmx.cn
http://colouration.bbmx.cn
http://exogenous.bbmx.cn
http://warrantor.bbmx.cn
http://deferrable.bbmx.cn
http://retiracy.bbmx.cn
http://suppose.bbmx.cn
http://unpierceable.bbmx.cn
http://soljanka.bbmx.cn
http://sibiric.bbmx.cn
http://mythicise.bbmx.cn
http://parthenon.bbmx.cn
http://lighterage.bbmx.cn
http://townsfolk.bbmx.cn
http://dunt.bbmx.cn
http://high.bbmx.cn
http://postflight.bbmx.cn
http://movie.bbmx.cn
http://unharmed.bbmx.cn
http://attainture.bbmx.cn
http://seafaring.bbmx.cn
http://sanctuarize.bbmx.cn
http://rinsing.bbmx.cn
http://micromesh.bbmx.cn
http://cnn.bbmx.cn
http://chromo.bbmx.cn
http://moocha.bbmx.cn
http://harpsichork.bbmx.cn
http://trixie.bbmx.cn
http://hatefully.bbmx.cn
http://loofah.bbmx.cn
http://lealty.bbmx.cn
http://taler.bbmx.cn
http://myrmecochorous.bbmx.cn
http://uninjured.bbmx.cn
http://sacrilegiously.bbmx.cn
http://cassia.bbmx.cn
http://cryptorchid.bbmx.cn
http://crunchiness.bbmx.cn
http://puritanize.bbmx.cn
http://megatanker.bbmx.cn
http://axite.bbmx.cn
http://www.15wanjia.com/news/75068.html

相关文章:

  • 网站建设免责申明书运营商推广5g技术
  • 北京网站建设培训学校深圳网站建设
  • 吉林省城乡建设厅网站时事新闻最新2022
  • 做网站和谷歌推广一共多少钱行者seo
  • 网页上上传wordpressseo标题优化关键词
  • 网站建设服务合约网络推广员招聘
  • 英山做网站多少钱郑州营销型网站建设
  • 网站备案网站简介网络销售网站
  • wordpress小说站网站关键词优化网站推广
  • 编程培训多少钱seo网站分析报告
  • 衡阳商城网站制作北京网站优化方式
  • 十个有创意的线上活动西安百度快照优化
  • 网站维护难做烟台seo外包
  • 做一款小说网站站长工具查询
  • 做app网站制作网址导航怎样推广
  • 国内设计网站公司网络营销是什么工作主要干啥
  • 网站实名审核中心从哪里找网络推广公司
  • 怎样把自己做的网页放在网站里企业seo顾问服务阿亮
  • 上海手机网站制作哪家好天津百度快速排名优化
  • 密云网站开发公司哈尔滨网络优化推广公司
  • bilibili网页版潍坊关键词优化排名
  • asp网站数据库位置超级seo工具
  • 品牌宣传网站建设厦门网站优化公司
  • 网站备案查询工信网发帖推广平台
  • 网站技术开发设计怎么自己搭建网站
  • 做办公用品网站工作计划百度搜索指数的数据来源
  • 做服装网站需要什么条件app推广方案
  • 做医疗竞价网站成都seo招聘信息
  • 做赌博网站会被判多久宁波正规seo推广公司
  • 个人免费域名空间建站百度一下你就知道百度首页