0

我有一个简单的文件上传网络服务作为我项目的一小部分。

这是我到目前为止在服务器端所做的:

    @POST
@Path("/file")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(List<Attachment> attachments,@Context HttpServletRequest request) {
    System.out.println("Got an attachment!");
    for(Attachment attr : attachments) {
        DataHandler handler = attr.getDataHandler();
        try {
            InputStream stream = handler.getInputStream();
            MultivaluedMap map = attr.getHeaders();
            OutputStream out = new FileOutputStream(new File("/home/yashdosi/s/" + getFileName(map)));    //getFileName is a seperate private function..

            int read = 0;
            byte[] bytes = new byte[1024];
            while ((read = stream.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }
            stream.close();
            out.flush();
            out.close();
        } catch(Exception e) {
          e.printStackTrace();
        }
    }

    return Response.ok("file uploaded").build();
}

当请求来自 html 表单时,它工作得非常好......当我尝试从 java 客户端发送请求时,它根本不起作用......!!

关于为此代码创建java客户端的任何想法..

这是我尝试使用的代码...可能此代码中有一个简单的错误,但是..我没有看到...正如我所说,此代码简单不起作用...没有错误或其他任何东西...。当我尝试在服务器控制台上打印某些内容以查看是否调用了服务时...它没有打印任何内容..所以我认为由于某种原因我无法联系该服务...

    public static void uploadPhoto() 
{
    String url = "http://localhost:8080/fileupload-ws/services/postdata";
    String output = null;
    PostMethod mPost = new PostMethod(url);
    HttpClient client = new HttpClient();

    try 
    {
        File imageFile = new File("/home/yashdosi/1.jpg");
        BufferedImage image = ImageIO.read(imageFile);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(image, "jpg", baos);
        byte[] encodedImage = Base64.encodeBase64(baos.toByteArray());

        String data = " " + " " + "" + "image/jpeg" + " " + "" + new String(encodedImage) + " " + "";

        mPost.setRequestBody(data);
        mPost.setRequestHeader("Content-Type", "text/xml");
        client.executeMethod( mPost );
        output = mPost.getResponseBodyAsString( );
        mPost.releaseConnection( );
    } catch (HttpException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println(output);
}
4

1 回答 1

0

终于有客户工作了!!

        HttpClient httpclient = new DefaultHttpClient();
    try {
        HttpPost httppost = new HttpPost("http://localhost:8080/fileupload-ws/services/postdata");

        FileBody img = new FileBody(new File("/home/yashdosi/1.jpg"));
        FileBody html = new FileBody(new File("/home/yashdosi/hotmail.html"));

        MultipartEntity reqEntity = new MultipartEntity();
        reqEntity.addPart("image", img);
        reqEntity.addPart("html", html);

        httppost.setEntity(reqEntity);
        httppost.setHeader("Content-Type", "multipart/form-data");

        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);
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    finally {
        try { httpclient.getConnectionManager().shutdown(); } catch (Exception ignore) {}
    }
于 2012-07-02T12:06:43.643 回答