首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对于HttpClient文件上传请求,ServletFileUpload.parseRequest()返回0项

对于HttpClient文件上传请求,ServletFileUpload.parseRequest()返回0项
EN

Stack Overflow用户
提问于 2014-02-22 16:41:21
回答 1查看 327关注 0票数 0

服务器代码使用ServletFileUpload:

代码语言:javascript
复制
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上传,具体如下:

代码语言:javascript
复制
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)
}
EN

回答 1

Stack Overflow用户

发布于 2014-02-23 14:28:51

我认为你应该像下面这样在你的android端使用MultiPart:

代码语言:javascript
复制
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());

请给我一些反馈

希望这能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21951916

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档