Spring IE에서 다중파일업로드 시 Stream ended unexpectedly 에러 + ajaxForm 한글 깨짐문제(UTF-8)

북마크 추가

일단 다중파일 업로드를 하는데 아래와 같은 방식을 사용 하였더니 익스플로러를 제외한 나머지 브라우저에서는 정상 동작한다.

 

익스플로러에서는 Stream ended unexpectedly 에러가 발생한다.

 

var form = new FormData(document.getElementById('uploadForm'));

 

$.ajax({

      url: "/soweb2/service/upload",

      data: form,

      dataType: 'text',

      processData: false,

      contentType: false,

      type: 'POST',

      success: function (response) {

 

      },

      error: function (jqXHR) {

          console.log(jqXHR');

      }

});

 

​그래서 jQuery ajaxForm plugin을 사용하기로 했다.

 

==================================================================

 

다운로드 :https://github.com/malsup/form/

 

라이센스 : MIT , GPL

 

You may use either license. The MIT License is recommended for most projects because it is simple and easy to understand and it places almost no restrictions on what you can do with the plugin.

If the GPL suits your project better you are also free to use the plugin under that license.

You don't have to do anything special to choose one license or the other and you don't have to notify anyone which license you are using. You are free to use the jQuery Form Plugin in commercial projects as long as the copyright header is left intact.

 

===================================================================

 

위에서 했던 ajax를

 

$("#uploadForm").ajaxForm({
    dataType:'text',

    cache: false,
    processData: false,
    contentType: false,
    success : function(response) {


    },
    error : function(jqXHR) {
         console.log(jqXHR);
         alert('error');
    }

});​

 

위와같이 변경했다. url은 (form에 action으로 바꿈)

 

컨트롤러에서는

 

@RequestMapping(value = "/upload", method = RequestMethod.POST)
 public void uploadFile(MultipartHttpServletRequest request ,HttpSession session) throws IOException {

 Iterator<String> itr =  request.getFileNames();
 List<MultipartFile> mpf = request.getFiles(itr.next());

          

 for(int i=0; i< mpf.size();i++){

                String path="경로생략";              
                File files = new File(path + mpf.get(i).getOriginalFilename());
                mpf.get(i).transferTo(files);
            }
}​

 

위와 같은 구조였는데 ajaxForm을 사용하니 ajax 호출 시 잘 되던 한글이 깨지는 문제 발생

 

분명 크롬에서 찍어보면 한글을 정상적으로 보내는데 컨트롤러에 넘어오면 깨지는 문제가 발생하여

URL인코딩도 해보고 여러가지 방법을 시도했으나 계속 깨짐

 

결국

File files = new File(path + mpf.get(i).getOriginalFilename()); 이부분을

 

String fileName = new String(mpf.get(i).getOriginalFilename().getBytes("8859_1"),"utf-8");
File files = new File(path + fileName );

 

위와 같이 변경하여 해결

AD
관리자
2014-10-08 14:39
SHARE