@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";
}
}
本地服务器上传到远程服务器
// 上传文件
FileSystemResource fileSystemResource = new FileSystemResource(fileNameWithPath);
if (!fileSystemResource.exists()) {//文件不存在处理
}
MediaType type = MediaType.parseMediaType("multipart/form-data");
headers.setContentType(type);
//headers.set("Cookie", cookies);//session用于验证身份
MultiValueMap<String, Object> form = new LinkedMultiValueMap<String, Object>();
form.add("file", fileSystemResource);
//form.add("pass", finalpass);;//用于验证身份
HttpEntity<MultiValueMap<String, Object>> files = new HttpEntity<>(form, headers);
responseEntity = restTemplate.postForEntity(urlTransfer, files, String.class);//发送
//此处要添加错误处理
String result = responseEntity.getBody().toString();
从远程服务器下载到本地服务器
HttpHeaders headers = new HttpHeaders();
HttpEntity<Resource> httpEntity = new HttpEntity<Resource>(headers);
ResponseEntity<byte[]> response = restTemplate.exchange(getFilePath, HttpMethod.GET, httpEntity, byte[].class);
if (response.getStatusCodeValue() != 200) {//异常处理
}
//写入本地
try {
File file = new File(path + File.separator + fileName);
FileOutputStream fos = new FileOutputStream(file);
fos.write(response.getBody());
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}