`
kanpiaoxue
  • 浏览: 1789372 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

Spring RestTemplate文件上传(Uploading MultipartFile with Spring RestTemplate)

 
阅读更多

 

文章《Uploading MultipartFile with Spring RestTemplate》

地址:https://www.baeldung.com/spring-rest-template-multipart-upload

代码:https://github.com/eugenp/tutorials/tree/master/spring-rest/src/main/java/com/baeldung/web/upload

client:

mport org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class MultipartFileUploadClient {

    public static void main(String[] args) throws IOException {
        uploadSingleFile();
        uploadMultipleFile();
    }

    private static void uploadSingleFile() throws IOException {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);

        MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
        body.add("file", getTestFile());


        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
        String serverUrl = "http://localhost:8082/spring-rest/fileserver/singlefileupload/";
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> response = restTemplate.postForEntity(serverUrl, requestEntity, String.class);
        System.out.println("Response code: " + response.getStatusCode());
    }

    private static void uploadMultipleFile() throws IOException {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);

        MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
        body.add("files", getTestFile());
        body.add("files", getTestFile());
        body.add("files", getTestFile());

        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
        String serverUrl = "http://localhost:8082/spring-rest/fileserver/multiplefileupload/";
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> response = restTemplate.postForEntity(serverUrl, requestEntity, String.class);
        System.out.println("Response code: " + response.getStatusCode());
    }

    public static Resource getTestFile() throws IOException {
        Path testFile = Files.createTempFile("test-file", ".txt");
        System.out.println("Creating and Uploading Test File: " + testFile);
        Files.write(testFile, "Hello World !!, This is a test file.".getBytes());
        return new FileSystemResource(testFile.toFile());
    }
}

 

 

server:

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/fileserver")
public class FileServerResource {

    @RequestMapping(path = "/singlefileupload/", method = RequestMethod.POST)
    public ResponseEntity<String> processFile(@RequestParam("file") MultipartFile file) throws IOException {

        byte[] bytes = file.getBytes();

        System.out.println("File Name: " + file.getOriginalFilename());
        System.out.println("File Content Type: " + file.getContentType());
        System.out.println("File Content:\n" + new String(bytes));

        return (new ResponseEntity<>("Successful", null, HttpStatus.OK));
    }

    @RequestMapping(path = "/multiplefileupload/", method = RequestMethod.POST)
    public ResponseEntity<String> processFile(@RequestParam("files") List<MultipartFile> files) throws IOException {

        for (MultipartFile file : files) {
            byte[] bytes = file.getBytes();

            System.out.println("File Name: " + file.getOriginalFilename());
            System.out.println("File Content Type: " + file.getContentType());
            System.out.println("File Content:\n" + new String(bytes));
        }

        return (new ResponseEntity<>("Successful", null, HttpStatus.OK));
    }
}

 

 

分享到:
评论

相关推荐

    SpringBoot上传文件实例下载

    在Spring Boot框架中,文件上传是一项常见的功能,用于接收用户通过Web界面提交的文件。Spring Boot结合了Spring MVC,使得处理文件上传变得简单且高效。本文将深入探讨如何在Spring Boot应用中实现单文件和批量文件...

    file-uploading-with-php-and-mysql.rar_file upload_truth5fw

    标题中的“file-uploading-with-php-and-mysql.rar_file upload_truth5fw”暗示了这是一个关于使用PHP和MySQL实现文件上传功能的教程或代码示例。PHP是一种广泛使用的服务器端脚本语言,尤其适合Web开发,而MySQL是...

    SpringBoot实现文件上传和下载功能源码

    如果需要处理大文件上传,还需添加`spring-boot-starter-data-jpa`依赖,因为它包含了处理多部分请求的`MultipartFile`类。 2. **配置文件** 在`application.properties`或`application.yml`中,我们可以设置文件...

    ext上传uploading

    标题“ext上传uploading”指的是基于EXTJS框架的文件上传功能。EXTJS是一个强大的JavaScript库,主要用于构建富客户端应用程序,其强大的组件模型使得创建复杂的Web界面变得简单。在这个场景中,“uploading”意味着...

    filedemo.rar

    首先,我们需要了解Spring Boot中的MultipartFile接口,这是Spring MVC提供的一个用于处理文件上传的核心接口。在控制器层,我们可以创建一个带有MultipartFile参数的方法来接收上传的文件。例如: ```java @...

    上传文件插件

    19. **Uploading Files with AJAX**:使用jQuery实现无刷新文件上传,简化前端代码。 20. **AJAX Multiple File Upload Form Using jQuery**:支持多文件同时上传,使用Ajax技术,避免页面刷新。 21. **...

    php文件上传模块,实现文件的上传

    此外,如果文件上传涉及大量用户或大文件,我们还需要考虑性能优化,比如使用异步上传、分块上传、进度条显示等技术。对于大文件,还可以通过切割文件后分别上传,再在服务器端合并。 最后,为了提高用户体验,我们...

    【JavaScript源代码】jquery+springboot实现文件上传功能.docx

    ### 使用jQuery与SpringBoot实现文件上传功能 #### 一、前言 在现代Web开发中,文件上传是一项常见的需求。无论是用户头像上传还是文档管理,都需要开发者掌握如何处理文件上传的技术。本篇文章将详细介绍如何利用...

    基于nestjs与vue3的大文件上传.zip

    在现代Web应用中,大文件上传是一个常见的需求,特别是在企业级项目中,用户可能需要上传GB级别的文件。本教程将探讨如何使用NestJS后端框架和Vue3前端框架实现高效、安全的大文件上传功能。 NestJS是基于...

    Springboot通过MinIO进行文件操作代码

    在Spring Boot应用中,MinIO是一个非常流行的对象存储服务,它可以用来进行文件的上传、下载、删除等操作。MinIO是高度兼容Amazon S3的对象存储服务器,提供了简单、快速、可扩展的数据存储解决方案。本教程将详细...

    django+ajaxfileupload文件上传demo

    在本文中,我们将深入探讨如何使用Django框架与AjaxFileUpload库实现一个文件上传的示例。Django是一个流行的Python Web开发框架,它提供了一系列强大的功能来构建高效、安全的Web应用。AjaxFileUpload则是一个...

    基于structs+hibernate 文件上传

    在Struts2中,文件上传主要依赖于`struts2-convention-plugin`和`struts2-file-uploading-plugin`这两个插件。用户通常需要配置`struts.xml`文件,定义一个Action类来接收文件,并在Action类中声明文件参数。 2. **...

    Strut2 文件上传和下载

    使用Struts2进行文件上传,需要引入Struts2的文件上传插件,如`struts2-convention-plugin`和`struts2-file-uploading-plugin`。 3. **Action类** 创建一个Action类,添加`File`和`String`类型的属性,如`private...

    spring boot 图片上传与显示功能实例详解

    在Spring Boot中实现图片上传与显示功能涉及到多个关键步骤,包括处理HTTP请求、存储上传的文件、以及在需要时提供文件的访问路径。下面将详细解释这个过程。 首先,Spring Boot默认内嵌了Tomcat服务器,它并不像...

    Stream跨域上传文件

    这里的`@CrossOrigin`注解用于设置CORS策略,`@RequestParam("file") MultipartFile file`用于接收前端上传的文件。 在实际应用中,还需要考虑安全性问题,例如防止CSRF攻击、验证文件类型和大小、处理并发上传等。...

    javaweb的文件上传整理

    struts.messages.error.uploading=文件上传失败 struts.messages.error.file.too.large=文件过大 struts.messages.error.content.type.not.allowed=不支持的文件类型 struts.messages.error.file.extension.not....

    Struts2实现单个文件多个文件上传与下载-多个拦截器

    在Struts2中,文件上传主要依赖于`struts2-convention-plugin`和`struts2-file-uploading-plugin`这两个插件。要实现文件上传,你需要在Action类中定义一个字段,类型为`java.io.File`或`org.apache.struts2....

    nginx-upload文件上传代码和工具

    `nginx-upload`模块是Nginx的一个第三方插件,由Uploading.com公司开发,它允许用户通过HTTP POST或PUT方法在服务器上上传大文件,同时支持断点续传和文件分块上传。这个模块的核心优点是能够减轻应用程序服务器的...

    Struts2框架实现文件上传

    实现文件上传,首先需要引入Struts2的上传插件,即`struts2-convention-plugin`和`struts2-file-uploading-plugin`。这些插件提供了处理文件上传所需的类和方法。 2. **配置Struts2 XML**: 在`struts.xml`配置...

    PHP+js文件上传

    在IT领域,文件上传是网站和应用程序中常见的一项功能,让用户能够方便地将本地文件传输到服务器。在“PHP+js文件上传”这个主题中,我们将深入探讨如何结合这两种技术来实现一个完整的文件上传系统,同时展示上传...

Global site tag (gtag.js) - Google Analytics