本地
@Controller
@RequestMapping(value = "/client")
public class FileServerAtClient {
@RequestMapping(value = "/upload1", method = RequestMethod.POST)
public @ResponseBody String upload1() {
String url = "http://restAddr/lyy-test/service/upload1";
String filePath = "E:/fileServer/client/upload/upload.png";
RestTemplate rest = new RestTemplate();
FileSystemResource resource = new FileSystemResource(new File(filePath));
MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
param.add("file", resource);
param.add("fileName", "xiaolian.png");
// method 1
String string = rest.postForObject(url, param, String.class);
// method 2
// HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String,Object>>(param);
// ResponseEntity<String> responseEntity = rest.exchange(url, HttpMethod.POST, httpEntity, String.class);
// String string = responseEntity.getBody();
System.out.println(string);
return "success";
}
@RequestMapping(value = "/download1", method = RequestMethod.GET)
public @ResponseBody String download1() {
String url = "http://restAddr/project/service/download1/kaola";
String filePath = "E:/fileServer/client/download/kaola.jpg";
InputStream inputStream = null;
OutputStream outputStream = null;
RestTemplate restTemplate = new RestTemplate();
try {
// Object result = restTemplate.getForObject(url, Object.class);
HttpHeaders headers = new HttpHeaders();
ResponseEntity<byte[]> response = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<byte[]>(headers), byte[].class);
byte[] result = response.getBody();
inputStream = new ByteArrayInputStream(result);
outputStream = new FileOutputStream(new File(filePath));
int len = 0;
byte[] buf = new byte[1024];
while ((len = inputStream.read(buf, 0, 1024)) != -1) {
outputStream.write(buf, 0, len);
}
outputStream.flush();
} catch (Exception e) {
e.printStackTrace();
return "error";
} finally {
try {
if(inputStream != null) inputStream.close();
if(outputStream != null) outputStream.close();
} catch (IOException e) {
e.printStackTrace();
System.err.println("数据流关闭异常!");
}
}
return "success";
}
}
远程服务
@Controller
@RequestMapping(value = "service")
public class FileServerAtService {
@SuppressWarnings("resource")
@RequestMapping(value = "/download1/{name}", method = RequestMethod.GET)
public void download1(@PathVariable("name") String name, HttpServletRequest request, HttpServletResponse response) throws Exception{
File file = new File("E:/fileServer/server/download/server.jpg");
if(file.isFile()){
try {
InputStream fileInput = new FileInputStream(file);
OutputStream out = response.getOutputStream();
byte[] buffer = new byte[4096];
int n = 0;
while (-1 != (n = fileInput.read(buffer)))
out.write(buffer, 0, n);
} catch (Exception e) {
e.printStackTrace();
System.err.println("Exception exit");
return ;
}
} else {
System.err.println("not found");
return ;
}
System.out.println("success end");
return ;
}
@RequestMapping(value = "/upload1", method = RequestMethod.POST)
public @ResponseBody String upload1(MultipartFile file, MultipartHttpServletRequest request){
// Map<String,String[]> requestParams = request.getParameterMap();
// MultiValueMap<String,MultipartFile> map = request.getMultiFileMap(); // map.get("file").get(0) ==file
String path = "E:/fileServer/server/upload/"+file.getOriginalFilename();
OutputStream out = null;
try {
out = new FileOutputStream(path);
Assert.notNull(file, "No input byte array specified");
out.write(file.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
if(out !=null) out.close();
}
catch (IOException ex) {
}
}
return "success";
}
}