Apache HttpClient 4.0 GA가 나와서 Commons에 있던 3.x을 버리고 적용을 해보았다. 처음에는 FileBody를 이용하여 파일 업로드 기능을 구현하니 큰 문제가 없었는데 spring MultipartFile을 이용하여 delegate하는 형식으로 파일 업로드를 하다보니 InputStream 밖에 없어서 InputStreamBody를 이용하니 Y모사 서버에서 content length가 정의되지 않았다며 불평을 늘어놓았다.
분석을 해보니 InputStreamBody의 getContentLength 함수는 항상 -1을 반환하는게 문제였다. 그리하여.. InputStreamBody를 상속하여 content length를 제대로 반환하는 모듈을 하나 만들었다.
1: import java.io.IOException;
2:
3: import org.apache.http.entity.mime.content.InputStreamBody;
4: import org.springframework.web.multipart.MultipartFile;
5:
6: public class MultipartFileBody extends InputStreamBody {
7:
8: private MultipartFile file;
9:
10: public MultipartFileBody(MultipartFile file) throws IOException {
11: super(file.getInputStream(), file.getContentType(), file.getOriginalFilename());
12:
13: this.file = file;
14: }
15:
16: public long getContentLength() {
17: return this.file.getSize();
18: }
19:
20: }
상위 모듈은 InputStreamBody를 상속받아서 getContentLength 부분만 override하여 제대로된 파일 크기를 반환하도록 하였다. 상속하니까 참 쉽죠잉?
댓글 없음:
댓글 쓰기