package kr.wisestone.owl.web.converter; import org.springframework.http.HttpInputMessage; import org.springframework.http.HttpOutputMessage; import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.http.converter.HttpMessageNotWritableException; import org.springframework.util.FileCopyUtils; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class FileHttpMessageConverter implements HttpMessageConverter { private List supportMediaTypes = new ArrayList(); public FileHttpMessageConverter() { this.supportMediaTypes.add(MediaType.APPLICATION_OCTET_STREAM); } @Override public boolean canRead(Class arg0, MediaType mediaType) { // TODO Auto-generated method stub return false; } @Override public boolean canWrite(Class clazz, MediaType mediaType) { // TODO Auto-generated method stub return File.class.equals(clazz); } @Override public List getSupportedMediaTypes() { // TODO Auto-generated method stub return supportMediaTypes; } @Override public void write(File file, MediaType mediaType, HttpOutputMessage message) throws IOException, HttpMessageNotWritableException { if(mediaType == null){ mediaType = MediaType.APPLICATION_OCTET_STREAM; } message.getHeaders().setContentType(mediaType); FileInputStream fis = null; try { fis = new FileInputStream(file); FileCopyUtils.copy(fis, message.getBody()); } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if (fis != null) { fis.close(); } } } @Override public File read(Class arg0, HttpInputMessage arg1) throws IOException, HttpMessageNotReadableException { // TODO Auto-generated method stub return null; } }