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

做网站 如何 挣钱兰州网站的建设

做网站 如何 挣钱,兰州网站的建设,wordpress 导航地图,做网站要几个人 要多少钱在Spring Boot项目中实现文件的上传、下载和预览功能#xff0c;可以通过使用Spring MVC的MultipartFile接口来处理文件上传#xff0c;并使用HttpServletResponse或Resource来实现文件下载和预览。下面是如何实现这些功能的完整示例。 1. 引入依赖 确保在pom.xml中引入了S…在Spring Boot项目中实现文件的上传、下载和预览功能可以通过使用Spring MVC的MultipartFile接口来处理文件上传并使用HttpServletResponse或Resource来实现文件下载和预览。下面是如何实现这些功能的完整示例。 1. 引入依赖 确保在pom.xml中引入了Spring Boot的相关依赖。通常情况下Spring Boot Starter Web已经包含了必要的依赖。 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId /dependency2. 创建文件上传、下载和预览的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 ResponseEntityResource 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 ResponseEntityResource 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.properties或application.yml中配置文件的上传路径 file.upload-dirC:/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-size100MB spring.servlet.multipart.max-request-size100MB或者在application.yml中 spring:servlet:multipart:max-file-size: 100MBmax-request-size: 100MB7. 文件预览类型支持 通常情况下浏览器支持预览的文件类型包括图片如jpeg、png、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://www.w-s-a.com/news/669424/

相关文章:

  • python做网站 不适合单页营销分享网站
  • 珠海市研发网站建设建设网站挣钱
  • 阿里巴巴国际站特点做wps的网站赚钱
  • wordpress更换域名后网站打不开宜昌建设银行网站
  • 写出网站开发的基本流程百度网页电脑版入口
  • 网站设计有限公司怎么样网站建设西班牙语
  • 网站安全解决方案宁波seo网络推广优化价格
  • 做网站带来好处wordpress可以做oa系统吗
  • 建筑设计人才招聘网站h5营销型网站suteng
  • 做app和网站怎样如何做html网站
  • php开发手机端网站开发更换网站标题
  • 提供网站建设报价延津县建设局网站
  • 江苏网站建设流程土巴兔全包装修怎么样
  • 环保网站建设方案带漂浮广告的网站
  • 淘宝客合伙人网站建设建站前端模板
  • 网站单页模板怎么安装中世纪变装小说wordpress
  • 手机免费建设网站制作宝安第一网站
  • 如何做x响应式网站asp网站出现乱码
  • 网站备案的幕布是什么来的游戏推广代理
  • 固始城乡建设局的网站怎么打不开了上海建设网站
  • 关于加强网站信息建设的通知3d网站开发成本
  • 网站建设实训过程报告成品网站1688入口的功能介绍
  • 网站定制开发需要什么资质国外设计灵感网站
  • 搜搜网站收录广告设计与制作模板图片
  • 江苏省建设监理协会网站汕头网站建设方案优化
  • 中国风网站配色方案正规少儿编程排名
  • 兼职做网站的软件wordpress赞的代码
  • 销售网站的技巧四博互联做的网站
  • 网站建设 图片问题小程序免费制作平台凡科网页版
  • 猪八戒网做网站怎么样网站建设 客户同程