The premise

1. The form request must be POST
2. Enctype ="multipart/form-data" encType ="multipart/form-data"
3. Importing dependencies: commons-upload, commons-io
Copy the code

1. Upload the file to the current server

A. Introduce dependencies

<! -- fileUpload will automatically depend on Commons --> 
<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.1</version>
</dependency>
Copy the code

B, spring-mVC.xml configuration file

<! -- Config file upload resolver -->
<! -- id is fixed -->
<bean id="multipartResolver"
				class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<! -- Set the maximum size of uploaded files to 5MB -->
		<property name="maxUploadSize">
			<value>5242880</value>
		</property>
</bean>
Copy the code

C. Page configuration

 <form action="${pageContext.request.contextPath}/upload" method="post" enctype="multipart/form-data">
        <input type="file" name="uploadFile">
        <input type="text" name="username">
        <input type="submit" value="Upload">
    </form>
Copy the code

D. Controller code

@Controller()
public class UploadController {

    /** * Upload MultipartFile Byte [] getBytes() converts a File into a byte array. -void transferTo(File File) Converts the uploaded File to a File object */
    @RequestMapping("/upload")
    public String upload(MultipartFile uploadFile, HttpServletRequest request){

        // A 32-bit random content string
        String uuid = UUID.randomUUID().toString().replace("-"."");
        // File name
        String filename = uploadFile.getOriginalFilename();
        System.out.println("filename = " + filename);

        // File upload
        String realPath = request.getSession().getServletContext().getRealPath(request.getContextPath() + "/upload");
        File path = new File(realPath, uuid+filename);
        try {
            uploadFile.transferTo(path);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "success"; }}Copy the code

2. Cross-service upload

A. Introduce dependencies

<! Jersey server package -->
    <dependency>
      <groupId>com.sun.jersey</groupId>
      <artifactId>jersey-core</artifactId>
      <version>1.18.1</version>
    </dependency>
    <dependency>
      <groupId>com.sun.jersey</groupId>
      <artifactId>jersey-client</artifactId>
      <version>1.18.1</version>
    </dependency>
Copy the code

B. Modify the Tomcat configuration

Tomcat cannot be uploaded across servers by default. 2. Tomcat /conf/web. XML <! -- <init-param> <param-name> Readonly </param-name> <param-name> false</param-value> </init-param>Copy the code

C. Configure the picture server

1. Create a Web project. 2. Create an Upload directory in the webApp directory. Empty folders will not compile, so you need to add (any) files to the upload directoryCopy the code

D. Modify the Controller code

   /** * MultipartFile interface method: -string getOriginalFilename() gets the file name of the upload file. -byte [] getBytes() converts the file into a byte array */
    @RequestMapping("/upload")
    public String upload(MultipartFile uploadFile, HttpServletRequest request){

        // A 32-bit random content string
        String uuid = UUID.randomUUID().toString().replace("-"."");
        // File name
        String filename = uploadFile.getOriginalFilename();
        System.out.println("filename = " + filename);

        // Upload across servers
        String serverUrl = "http://localhost:8081/upload";
        // The client object to upload to the server
        Client client = Client.create();
        WebResource resource = client.resource(serverUrl + "/" + uuid + filename);
        // Upload the file to the specified server
        // Roll out the byte array to start uploading
        try {
            resource.put(String.class, uploadFile.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "success";
    }
Copy the code