How to use Apache FileUppload and HttpClient?

心已入冬 提交于 2019-12-11 18:34:24

问题


I am writing some code to send a log file via http to a server if a JAWS app fails. To test this I have setup a servlet that receives and processes the request.

My Client code is as from the Apache HttpClient 4.2 examples

{
        HttpClient httpclient = new DefaultHttpClient();
        try {
            HttpPost httppost = new HttpPost("http://localhost:8080/testServer/testserv/a");

            FileBody bin = new FileBody(new File("XXXs"));
            StringBody comment = new StringBody("A binary file of some kind");

            MultipartEntity reqEntity = new MultipartEntity();
            reqEntity.addPart("bin", bin);
            reqEntity.addPart("comment", comment);

            httppost.setEntity(reqEntity);

            System.out.println("executing request " + httppost.getRequestLine());
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity resEntity = response.getEntity();

            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            if (resEntity != null) {
                System.out.println("Response content length: " + resEntity.getContentLength());
            }
            EntityUtils.consume(resEntity);
        } finally {
            try { httpclient.getConnectionManager().shutdown(); } catch (Exception ignore) {}
        }
    }
    {
        DefaultHttpClient httpclient = new DefaultHttpClient();

        HttpPost httpPost = new HttpPost("http://localhost:8080/testServer/testserv/a");
        List <NameValuePair> nvps = new ArrayList <NameValuePair>();
        nvps.add(new BasicNameValuePair("username", "vip"));
        nvps.add(new BasicNameValuePair("password", "secret"));
        httpPost.setEntity(new UrlEncodedFormEntity(nvps));
        HttpResponse response2 = httpclient.execute(httpPost);

        try {
            System.out.println(response2.getStatusLine());
            HttpEntity entity2 = response2.getEntity();
            // do something useful with the response body
            // and ensure it is fully consumed
            EntityUtils.consume(entity2);
        } finally {
            httpPost.releaseConnection();
        }
    }

My server side uses the tutorial for Apache FileUpload

boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    if(isMultipart){
        // Create a factory for disk-based file items
        FileItemFactory factory = new DiskFileItemFactory();

        // Create a new file upload handler
        ServletFileUpload upload = new ServletFileUpload(factory);

        // Parse the request
        try {
            List<FileItem> items = upload.parseRequest(request);
            Iterator iter = items.iterator();

            while (iter.hasNext()) {
                FileItem item = (FileItem) iter.next();



                File uploadedFile = new File("XXX");
                try {
                    item.write(uploadedFile);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println(uploadedFile.getAbsolutePath());
            }
        } catch (FileUploadException e) {
            System.out.println("File Upload Exception");
            e.printStackTrace();
        }

    } else{
        System.out.println("Not multi part");
        System.out.println(request.getParameterMap());
    }

I am using JBoss AS7. The outcome is that if I do not add the FileEntity to the MultiPartEntity the request is processed. Other wise a FileUpload exception is thrown.

Processing of multipart/form-data request failed. Stream ended unexpectedly 

I am not very experienced in using JBoss and the other tools here so you're help and patience is greatly appreciated.


回答1:


From this question I learnt that you need to have apache-commons-io as a dependency.



来源:https://stackoverflow.com/questions/13073877/how-to-use-apache-fileuppload-and-httpclient

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!