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

中小企业网站积木式搭建wordpress 侧边栏代码

中小企业网站积木式搭建,wordpress 侧边栏代码,简单网站建设,文创产品设计案例及理念前言#xff1a;这边汇总了一下目前SpringBoot项目当中常见文件上传和下载的功能#xff0c;一共三种常见的下载方式和一种上传方式#xff0c;特此做一个笔记分享。 目录 一、pom依赖 二、yml配置文件 三、文件下载 3.1、使用Spring框架提供的下载方式 3.2、通过IOUti… 前言这边汇总了一下目前SpringBoot项目当中常见文件上传和下载的功能一共三种常见的下载方式和一种上传方式特此做一个笔记分享。 目录 一、pom依赖 二、yml配置文件 三、文件下载 3.1、使用Spring框架提供的下载方式 3.2、通过IOUtils以流的形式下载 3.3、边读边下载 四、文件上传 五、工具类完整代码 六、Gitee源码  七、总结 一、pom依赖 dependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency/dependencies 二、yml配置文件 # Spring配置 spring:# 文件上传servlet:multipart:# 单个文件大小max-file-size: 10MB# 设置总上传的文件大小max-request-size: 20MB server:port: 9090三、文件下载 3.1、使用Spring框架提供的下载方式 关键代码 /*** 使用Spring框架自带的下载方式* param filePath* param fileName* return*/public ResponseEntityResource download(String filePath,String fileName) throws Exception {fileName URLEncoder.encode(fileName,UTF-8);File file new File(filePath);if(!file.exists()){throw new Exception(文件不存在);}return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION,attachment; filename fileName ).body(new FileSystemResource(filePath));} 请求层 RestController RequestMapping(/file) public class FileController {Autowiredprivate FileUtil fileUtil;GetMapping(/spring/download)public ResponseEntityResource download() throws Exception {String filePath D:\\1.jpg;String fileName Spring框架下载.jpg;return fileUtil.download(filePath,fileName);}} 浏览器输入http://localhost:9090/file/spring/download  下载完成。  3.2、通过IOUtils以流的形式下载 关键代码 /*** 通过IOUtils以流的形式下载* param filePath* param fileName* param response*/public void download(String filePath , String fileName, HttpServletResponse response) throws Exception {fileName URLEncoder.encode(fileName,UTF-8);File filenew File(filePath);if(!file.exists()){throw new Exception(文件不存在);}response.setHeader(Content-disposition,attachment;filename fileName);FileInputStream fileInputStream new FileInputStream(file);IOUtils.copy(fileInputStream,response.getOutputStream());response.flushBuffer();fileInputStream.close();} 请求层  RestController RequestMapping(/file) public class FileController {Autowiredprivate FileUtil fileUtil;GetMapping(/io/download)public void ioDownload(HttpServletResponse response) throws Exception {String filePath D:\\1.jpg;String fileName IO下载.jpg;fileUtil.download(filePath,fileName,response);}} 浏览器访问http://localhost:9090/file/io/download 下载成功。  3.3、边读边下载 关键代码 /*** 原始的方法下载一些小文件边读边下载的* param filePath* param fileName* param response* throws Exception*/public void downloadTinyFile(String filePath,String fileName, HttpServletResponse response)throws Exception{File file new File(filePath);fileName URLEncoder.encode(fileName, UTF-8);if(!file.exists()){throw new Exception(文件不存在);}FileInputStream in new FileInputStream(file);response.setHeader(Content-Disposition, attachment;filenamefileName);OutputStream out response.getOutputStream();byte[] b new byte[1024];int len 0;while((len in.read(b))!-1){out.write(b, 0, len);}out.flush();out.close();in.close();} 请求层 RestController RequestMapping(/file) public class FileController {Autowiredprivate FileUtil fileUtil;GetMapping(/tiny/download)public void tinyDownload(HttpServletResponse response) throws Exception {String filePath D:\\1.jpg;String fileName tiny下载.jpg;fileUtil.downloadTinyFile(filePath,fileName,response);}}浏览器输入http://localhost:9090/file/tiny/download  下载成功。 四、文件上传 使用MultipartFile上传文件 /*** 上传文件* param multipartFile* param storagePath* return* throws Exception*/public String upload(MultipartFile multipartFile, String storagePath) throws Exception{if (multipartFile.isEmpty()) {throw new Exception(文件不能为空);}String originalFilename multipartFile.getOriginalFilename();String newFileName UUID.randomUUID()_originalFilename;String filePath storagePathnewFileName;File file new File(filePath);if (!file.getParentFile().exists()) {file.getParentFile().mkdirs();}multipartFile.transferTo(file);return filePath;} 请求层 RestController RequestMapping(/file) public class FileController {Autowiredprivate FileUtil fileUtil;PostMapping(/multipart/upload)public String download(MultipartFile file) throws Exception {String storagePath D:\\;return fileUtil.upload(file,storagePath);}} 使用postman工具测试 在D盘找到此文件。  五、工具类完整代码 package com.example.file.utils;import org.apache.tomcat.util.http.fileupload.IOUtils; import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; import org.springframework.http.HttpHeaders; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.net.URLEncoder; import java.util.UUID;/*** 文件工具类* author HTT*/ Component public class FileUtil {/*** 使用Spring框架自带的下载方式* param filePath* param fileName* return*/public ResponseEntityResource download(String filePath,String fileName) throws Exception {fileName URLEncoder.encode(fileName,UTF-8);File file new File(filePath);if(!file.exists()){throw new Exception(文件不存在);}return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION,attachment; filename fileName ).body(new FileSystemResource(filePath));}/*** 通过IOUtils以流的形式下载* param filePath* param fileName* param response*/public void download(String filePath , String fileName, HttpServletResponse response) throws Exception {fileName URLEncoder.encode(fileName,UTF-8);File filenew File(filePath);if(!file.exists()){throw new Exception(文件不存在);}response.setHeader(Content-disposition,attachment;filename fileName);FileInputStream fileInputStream new FileInputStream(file);IOUtils.copy(fileInputStream,response.getOutputStream());response.flushBuffer();fileInputStream.close();}/*** 原始的方法下载一些小文件边读边下载的* param filePath* param fileName* param response* throws Exception*/public void downloadTinyFile(String filePath,String fileName, HttpServletResponse response)throws Exception{File file new File(filePath);fileName URLEncoder.encode(fileName, UTF-8);if(!file.exists()){throw new Exception(文件不存在);}FileInputStream in new FileInputStream(file);response.setHeader(Content-Disposition, attachment;filenamefileName);OutputStream out response.getOutputStream();byte[] b new byte[1024];int len 0;while((len in.read(b))!-1){out.write(b, 0, len);}out.flush();out.close();in.close();}/*** 上传文件* param multipartFile* param storagePath* return* throws Exception*/public String upload(MultipartFile multipartFile, String storagePath) throws Exception{if (multipartFile.isEmpty()) {throw new Exception(文件不能为空);}String originalFilename multipartFile.getOriginalFilename();String newFileName UUID.randomUUID()_originalFilename;String filePath storagePathnewFileName;File file new File(filePath);if (!file.getParentFile().exists()) {file.getParentFile().mkdirs();}multipartFile.transferTo(file);return filePath;}}六、Gitee源码  码云地址SpringBoot实现文件上传和下载 七、总结 以上就是SpringBoot实现文件上传和下载功能的笔记一键复制使用即可。
http://www.zqtcl.cn/news/284647/

相关文章:

  • 网站建设管理教程视频教程如何建一个电商平台
  • 深圳网络公司做网站wordpress怎么编辑网站
  • 淄博建站网络公司wordpress ldap外部登录认证
  • 南宁网站开发浏览器有哪几种
  • 石家庄哪里能制作网站网站301跳转
  • 网站建设报价浩森宇特wordpress怎样修改字体
  • 网站建设预估费用做品牌推广用什么网站
  • 网站建设风险评估西部数码 空间做2个网站
  • 专业购物网站建设wordpress the7不显示分类页
  • 移动应用开发网站建设网站时的故障分类
  • 网站动态静态软件项目管理案例教程第四版
  • 贵州萝岗seo整站优化鲜花店网站建设的总结
  • 下载做网站的软件建网站做站在
  • 无锡高端网站建设公司WordPress臃肿主题
  • 网站建设与运营财务预算seo下拉优化
  • 重庆铜梁网站建设价格阜城网站建设价格
  • 怎样建置换平台网站公众号开发周期
  • 朝阳建设网站什么是网络设计方案网络设计的原则有哪些
  • 长春商城网站制作二级网站建设 知乎
  • 网站建设的结论沭阳县建设局网站
  • 镇江网站制作价格网络有限公司简介
  • 海淀网站建设哪家公司好wordpress非常卡
  • 门户网站的建设意义交互设计专业就业前景
  • 那里有学做网站的2345网址导航下载官网
  • 房产证查询系统官方网站购买网站域名
  • 高端企业门户网站建设服务公司深圳企业网站怎么做
  • 页游网站如何做推广平面图设计软件有哪些
  • 自建网站有哪些wordpress 评论增加字段
  • 企业网站建设的方案书pc网站 公众号数据互通
  • 东莞设计制作网站制作做的asp网站手机号码