ServletFileUpload.parseRequest()为HttpClient文件上传请求返回0项

时间:2014-02-22 08:41:21

标签: android servlets androidhttpclient

服务器代码使用ServletFileUpload:

public void doPost(HttpServletRequest request, HttpServletResponse response)  
               throws ServletException, IOException {  
        this.createLog("F:\\Program Files\\Apache Software Foundation\\Tomcat 6.0\\webapps\\photolistservice\\log\\", "test.txt", "cc5zhenhua.log started");
        this.writerLogInfo(logPrint, "***************************************************************************************************");
        PrintWriter out= response.getWriter();
        boolean isMultipart = ServletFileUpload.isMultipartContent(request);
        if(isMultipart)
            this.writerLogInfo(logPrint, "isMultipart=true");
        else
            this.writerLogInfo(logPrint, "isMultipart=false");
        //Create a factory for disk-based file items
        DiskFileItemFactory factory = new DiskFileItemFactory();
        factory.setSizeThreshold(4*1024*1024);
        // Configure a repository (to ensure a secure temp location is used)

        //ServletContext servletContext = this.getServletConfig().getServletContext();
        File repository = new File("F:\\Program Files\\Apache Software Foundation\\Tomcat 6.0\\webapps\\photolistservice\\temp");
        factory.setRepository(repository);

        this.writerLogInfo(logPrint, "after factory.setRository");
        List<FileItem> items = new ArrayList<FileItem>();
        try {
        // Create a new file upload handler
        ServletFileUpload upload = new ServletFileUpload(factory);
        this.writerLogInfo(logPrint, "after new servlet file upload");


           // Parse the request 

            items = upload.parseRequest(request);
....}

客户端使用android HttpClient进行上传,如下所示:

private boolean uploadfile(String filepath, String actionURL) {

        String end = "\r\n";
        String twoHyphens = "--";
        String boundary = java.util.UUID.randomUUID().toString();
        try {
              HttpClient httpclient = new DefaultHttpClient();
                httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

                HttpPost httppost = new HttpPost(actionURL);
                File file = new File(filepath);

                FileEntity reqEntity = new FileEntity(file, "multipart/form-data");


                httppost.setEntity(reqEntity);
                reqEntity.setContentType("multipart/form-data;boundary="+boundary);
               // reqEntity.setContentType(multipart/form-data);
                System.out.println("executing request " + httppost.getRequestLine());
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity resEntity = response.getEntity();

                System.out.println(response.getStatusLine());
                if (resEntity != null) {
                  System.out.println(EntityUtils.toString(resEntity));
                }
                if (resEntity != null) {
                  resEntity.consumeContent();
                }

                httpclient.getConnectionManager().shutdown();   
                return true;
        } catch (Exception e)
}

1 个答案:

答案 0 :(得分:0)

我认为您应该在Android端使用MultiPart,如下所示:

HttpClient httpclient = new DefaultHttpClient();

HttpPost httppost = new HttpPost(actionURL);


MultipartEntity entity = new MultipartEntity();

entity.addPart("name", new StringBody("fileName", Charset.forName("UTF-8")));
File myFile = new File(filepath);
FileBody fileBody = new FileBody(myFile);
entity.addPart("file", fileBody);
httppost.setEntity(entity);
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
System.out.println(response.getStatusLine());

请给我一些反馈

希望有助于。