1、上传文件
@RestController
@RequestMapping("/file")
public class FileUploadController {
/**
* 对于上载文件,要将MultipartFile用作请求参数,
* @param file
* @return
* @throws IOException
*/
@RequestMapping(value = "/upload", method = RequestMethod.POST,
consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public String fileUpload(@RequestParam("file") MultipartFile file) throws IOException {
// 上传的路径
File convertFile = new File("/home/user/temp/"+file.getOriginalFilename());
convertFile.createNewFile();
FileOutputStream fout = new FileOutputStream(convertFile);
fout.write(file.getBytes());
fout.close();
return "File is upload successfully";
}
}
2、下载文件
/**
* 对于文件下载,应该使用InputStreamResource下