1. 程式人生 > >SpringMVC返回檔案流

SpringMVC返回檔案流

@RequestMapping("message/get")
	@ResponseBody
	public ResponseEntity<byte[]> getMessageImage(@RequestParam(ImageRequestParam) String fileName)
			throws IOException {

		String filePath = FinalString.ImagePath + fileName;
		File file = new File(filePath);

		HttpHeaders headers = new HttpHeaders();
		headers.setContentDispositionFormData("fileName", EncodeingConverter.convertReturnToISO(fileName));
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);

		ResponseEntity<byte[]> result = new ResponseEntity<byte[]>(FileUtil.readAsByteArray(file), headers,
				HttpStatus.OK);

		return result;

	}
很方便咯。不多說了