0

プロジェクトのごく一部として、単純なファイルアップロードWebサービスがあります。

これは私がこれまでサーバー側で行ったことです:

    @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 に答える