Unexpected EOF read on the socket, multipartRequest로 파일을 서버로 보낼 때 서버에서 생긴 오류.

조회수 1808회

//클라이언트인 안드로이드 코드입니다.

protected String doInBackground(String... params) {
        String sdcard = Environment.getExternalStorageDirectory().getPath();
        Log.e("sdcard",sdcard);

        // 파일을 서버로 보내는 부분
        HttpClient client = new DefaultHttpClient();
        String url = "http://192.168.0.37:9080/AndroidWeb/index.jsp";
        HttpPost post = new HttpPost(url);

        //카메라 앨범에서 사진 가져올 때 이 방식 사용. FileBody에 파일을 받아 와서.
        // FileBody 객체를 이용해서 파일을 받아옴.
        File glee = new File("/storage/emulated/0/DCIM/Camera/20170210_110745.jpg"); // 파일을 받아 오는 부분.
//        File glee = new File(sdcard + "/Download/DetectFace.apk"); // 파일을 받아 오는 부분.
        FileBody bin = new FileBody(glee); // FileBody 생성

        MultipartEntityBuilder meb = MultipartEntityBuilder.create();
        meb.setCharset(Charset.forName("UTF-8"));
        meb.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        meb.addPart("images", bin); // 실제 파일을 multipart에 넣는다.
        HttpEntity entity = meb.build(); // meb를 토대로 HttpEntity를 생성한다.

        post.setEntity(entity); // entity를 post 형식에 담는다.

        try {
            HttpResponse reponse = client.execute(post); // post 형식의 데이터를 서버로 전달.
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } // post 형식의 데이터를 서버로 전달

        return "SUCCESS";
    }

//서버 JSP 코드이구요. 멀티 파트를 생성한 뒤에 에러가 발생하는것 같습니다. // multi = new MultipartRequest(request, realFolder, maxSize, encType, new DefaultFileRenamePolicy()); 제 생각엔 이부분이 문제로 보여지는데, 'Unexpected EOF read on the socket' 이런 오류가 나는 이유를 찾아보니 파일이 깨져서 그렇다고해서 다른 파일들로 테스트해 보았으나 같은 오류를 출력했습니다. 혹시 원인을 아시는 분 계시면 도와주시면 감사하겠습니다.

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@page
    import="com.oreilly.servlet.MultipartRequest,com.oreilly.servlet.multipart.DefaultFileRenamePolicy,java.util.*,java.io.*"%>
<form method="post" ENCTYPE="multipart/form-data">
<%
    //주의사항. 파일저장 정확한 경로아래에 폴더가 만들어져 있어야한다.
    //폼에서 넘길때 enctype="multipart/form-data"

    //정확한경로의 설정및 확인방법은 다음과같으며...
    String realFolder = ""; //파일경로를 알아보기위한 임시변수를 하나 만들고,
    String saveFolder = "filestorage"; //파일저장 폴더명을 설정한 뒤에...
    String encType = "euc-kr"; //인코딩방식도 함께 설정한 뒤,
    int maxSize = 100 * 1024 * 1024; //파일 최대용량까지 지정해주자.(현재 100메가)
    ServletContext context = getServletContext();
    realFolder = context.getRealPath(saveFolder);
    System.out.println("the realpath is : " + realFolder); // file path

    File dir = new File(realFolder); // 디렉토리 위치 지정
    if (!dir.exists()) { // 디렉토리가 존재하지 않으면
        dir.mkdirs(); // 디렉토리 생성.!
    }
    // print current time
    Date today = new Date();
    System.out.println(today);

    try {
        //멀티파트생성과 동시에 파일은 저장이 되고...
        MultipartRequest multi = null;
        System.out.println("1");

        // 오류 발생 지점.
        multi = new MultipartRequest(request, realFolder, maxSize, encType, new DefaultFileRenamePolicy());
        System.out.println("2");
        //이 시점을기해 파일은 이미 저장이 되었다.

        //폼에서 넘어왔던파일 파라메터들을 가져오려면 이렇게.
        Enumeration params = multi.getParameterNames();

        //그리고 가져온 파라메터를 꺼내는 방법...
        while (params.hasMoreElements()) {
            String name = (String) params.nextElement();//파라메터이름을 가져온뒤
            String value = multi.getParameter(name);//이름을 이용해  값을가져온다
            System.out.println(name + " = " + value);
            application.log(name + " = " + value); // logManager
        }

        //이번엔 파일과 관련된 파라메터를 가져온다.
        Enumeration files = multi.getFileNames();

        //이번엔 파일관련 파라메터를 꺼내본다...
        while (files.hasMoreElements()) {
            String name = (String) files.nextElement();//파라메터이름을 가져온뒤
            String filename = multi.getFilesystemName(name);//이름을 이용해 저장된 파일이름을 가져온다.
            String original = multi.getOriginalFileName(name);//이름을이용해 본래 파일이름도 가져온다.
            String type = multi.getContentType(name);//파일타입도 가져올수있다.
            File f = multi.getFile(name);//파일 용량을 알아보기위해서는 이렇게...
            System.out.println("Parameter Name: " + name);
            System.out.println("Real File Name: " + original);
            System.out.println("Saved File Name: " + filename);
            System.out.println("File Type: " + type);
            if (f != null) {
                System.out.println("File Size: " + f.length());
            }

            System.out.println("-------------------------------");

        }
    } catch (IOException ioe) {
        System.out.println(ioe);
    } catch (Exception ex) {
        System.out.println(ex);
    }
%>
```
  • (•́ ✖ •̀)
    알 수 없는 사용자

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)