下载了网站建设asp环球贸易网站

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

下载了网站建设asp,环球贸易网站,阳江seo,查网站有没有做推广Spring Boot中文件上传 前言 本篇主要参考Spring官方文档#xff0c;整理了Spring Boot中文件上传如何实现#xff0c;以及在代码中使用RestTemplate和HttpClient两种方式实现文件上传。 创建Spring Boot项目 首先创建一个Spring Boot Web项目#xff0c;使用的Spring B…Spring Boot中文件上传 前言 本篇主要参考Spring官方文档整理了Spring Boot中文件上传如何实现以及在代码中使用RestTemplate和HttpClient两种方式实现文件上传。 创建Spring Boot项目 首先创建一个Spring Boot Web项目使用的Spring Boot版本为2.6.14项目的pom文件如下 ?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdorg.yzh/groupIdartifactIduploadFile/artifactIdversion1.0-SNAPSHOT/versionpropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/propertiesparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.6.14/version/parentdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.apache.httpcomponents/groupIdartifactIdhttpclient/artifactId/dependencydependencygroupIdorg.apache.httpcomponents/groupIdartifactIdhttpmime/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependencydependencygroupIdjunit/groupIdartifactIdjunit/artifactIdscopetest/scope/dependency/dependencies /project主要功能 提供下面三个功能 方法URL功能POST/upload上传文件GET/files获取文件列表GET/files/{fileName}下载文件 具体实现 代码结构 主要功能实现 这边直接贴一下代码 controller /*** 文件上传** author yuanzhihao* since 2023/3/22/ RestController RequestMapping public class FileUploadController {Autowiredprivate FileUploadService fileUploadService;/** 上传文件** param files 文件* return 响应消息/PostMapping(/upload)public ResponseEntityString upload(RequestParam(files) MultipartFile[] files) {fileUploadService.upload(files);return ResponseEntity.ok(File Upload Success);}/** 获取文件列表** return 文件列表/GetMapping(/files)public ResponseEntityListFileInfo list() {return ResponseEntity.ok(fileUploadService.list());}/** 获取指定文件** param fileName 文件名称* return 文件/GetMapping(/files/{fileName:.})public ResponseEntityResource getFile(PathVariable(fileName) String fileName) {return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION,attachment; filename\ fileName ).body(fileUploadService.getFile(fileName));} } service /** 文件上传Service** author yuanzhihao* since 2023/3/27/ public interface FileUploadService {void upload(MultipartFile[] files);ListFileInfo list();Resource getFile(String fileName); }/** 文件上传** author yuanzhihao* since 2023/3/27*/ Service Slf4j public class FileUploadServiceImpl implements FileUploadService {Value(${upload.path:/data/upload/})private String filePath;private static final ListFileInfo FILE_STORAGE new CopyOnWriteArrayList();Overridepublic void upload(MultipartFile[] files) {SimpleDateFormat simpleDateFormat new SimpleDateFormat(yyyy-MM-dd HH:mm:ss);for (MultipartFile file : files) {String fileName file.getOriginalFilename();boolean match FILE_STORAGE.stream().anyMatch(fileInfo - fileInfo.getFileName().equals(fileName));if (match) {throw new RuntimeException(File [ fileName ] already exist);}String currentTime simpleDateFormat.format(new Date());try (InputStream in file.getInputStream();OutputStream out Files.newOutputStream(Paths.get(filePath fileName))) {FileCopyUtils.copy(in, out);} catch (IOException e) {log.error(File [{}] upload failed, fileName, e);throw new RuntimeException(e);}FileInfo fileInfo new FileInfo().setFileName(fileName).setUploadTime(currentTime);FILE_STORAGE.add(fileInfo);}}Overridepublic ListFileInfo list() {return FILE_STORAGE;}Overridepublic Resource getFile(String fileName) {FILE_STORAGE.stream().filter(info - info.getFileName().equals(fileName)).findFirst().orElseThrow(() - new RuntimeException(File [ fileName ] not exist));File file new File(filePath fileName);return new FileSystemResource(file);} }上传文件限制 可以在application.properties配置文件中限制上传单个文件的大小和所有文件的总大小具体配置如下

单个文件限制

spring.servlet.multipart.max-file-size20MB

总大小限制

spring.servlet.multipart.max-request-size100MB测试验证 使用postman进行接口测试 文件上传 正常上传文件 文件已存在 上传超过20MB的文件 上传总共超过100MB的文件
查询文件列表 文件下载 正常文件下载 下载不存在的文件
代码中调用上传接口 主要整理了使用restTemplate和httpclient客户端如何在代码中调用文件上传接口。 使用restTemplate调用上传文件接口 Test public void uploadTestByRestTemplate() {HttpHeaders headers new HttpHeaders();headers.setContentType(MediaType.MULTIPART_FORM_DATA);MultiValueMapString, Object body new LinkedMultiValueMap();File file new File(/Users/yuanzhihao/Downloads/mirrors-jenkins-master.zip);body.add(files, new FileSystemResource(file));body.add(files, new FileSystemResource(new File(/Users/yuanzhihao/Downloads/crictl-v1.22.0-linux-amd64.tar.gz)));body.add(files, new FileSystemResource(new File(/Users/yuanzhihao/Downloads/client(macosx).zip)));HttpEntityMultiValueMapString, Object requestEntity new HttpEntity(body, headers);String serverUrl http://localhost:8080/upload;RestTemplate restTemplate new RestTemplate();ResponseEntityString response restTemplate.postForEntity(serverUrl, requestEntity, String.class);System.out.println(Response code: response.getStatusCode() Response body: response.getBody()); }使用httpclient调用上传文件接口 Test public void uploadTestByHttpClient() {File file new File(/Users/yuanzhihao/Downloads/xzs-sql-v3.9.0.zip);FileBody fileBody new FileBody(file, ContentType.DEFAULT_BINARY);MultipartEntityBuilder builder MultipartEntityBuilder.create();builder.addPart(files, fileBody);HttpPost post new HttpPost(http://localhost:8080/upload);org.apache.http.HttpEntity entity builder.build();post.setEntity(entity);try (CloseableHttpClient client HttpClientBuilder.create().build();CloseableHttpResponse response client.execute(post)) {System.out.println(Response code: response.getStatusLine().getStatusCode());System.out.println(Response body: EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8));} catch (IOException e) {throw new RuntimeException(e);} }结语 代码地址https://github.com/yzh19961031/blogDemo/tree/master/uploadFile 参考 https://spring.io/guides/gs/uploading-files/ https://www.baeldung.com/spring-rest-template-multipart-upload https://www.bezkoder.com/spring-boot-file-upload/