1.创建一个控制层 FileController
package com.rainy.controller;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.util.UUID;
/**
* 文件上传
*/
@Controller
public class FileController {
private static String FILEPATH="http;//127.0.0.1:8086";
@GetMapping(value = "/file")
public String file() {
return "file";
}
@PostMapping(value = "/fileUpload")
public Map<String,String> fileUpload(@RequestParam("name") String name, @RequestPart("file") MultipartFile file, Model model, HttpServletRequest request) {
Map<String,String> map=new HashMap<>();
if (file.isEmpty()) {
return null;
}
String fileName = file.getOriginalFilename(); // 文件名
String suffixName = fileName.substring(fileName.lastIndexOf(".")); // 后坠名
fileName = UUID.randomUUID() + suffixName; // 新文件名
//创建jersey包中的client对象
Client client = Client.creat();
WwbResourse resourse = client.resourse(FILEPATH+ fileName);
//将文件保存到另一个服务器上
resourse.put(String.class,file.getBytes());
map.put("message","上传成功");
return map;
}
}