1

JAX RS を使用してファイル アップロードの例をセットアップしようとしています。プロジェクトをセットアップし、サーバーの場所にファイルを正常にアップロードできました。しかし、ファイルサイズが10KBを超えると、次のエラーが発生します(奇妙な!!)

com.sun.jersey.api.client.UniformInterfaceException: POST http://localhost:9090/DOAFileUploader/rest/file/upload returned a response status of 400
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:607)
at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)
at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:507)
at com.sony.doa.rest.client.DOAClient.upload(DOAClient.java:75)
at com.sony.doa.rest.client.DOAMain.main(DOAMain.java:34)

私は JAX RS が初めてで、正確に何が問題なのかわかりません。クライアント側またはサーバー側 (サイズ、タイムアウトなど) のパラメータを設定する必要がありますか?

これは、Web サービスを呼び出すクライアント側のコードです。

public void upload() {
    File file = new File(inputFilePath);
    FormDataMultiPart part = new FormDataMultiPart();
    part.bodyPart(new FileDataBodyPart("file", file, MediaType.APPLICATION_OCTET_STREAM_TYPE));
    WebResource resource = Client.create().resource(url);
    String response = resource.type(MediaType.MULTIPART_FORM_DATA_TYPE).post(String.class, part);
    System.out.println(response);
}

これはサーバー側のコードです:

@Path("/file")
public class UploadFileService {

@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
        @FormDataParam("file") InputStream uploadedInputStream,
        @FormDataParam("file") FormDataContentDisposition fileDetail) {

    String uploadedFileLocation = "e://uploaded/"
            + fileDetail.getFileName();

    writeToFile(uploadedInputStream, uploadedFileLocation);

    String output = "File uploaded to : " + uploadedFileLocation;

    return Response.status(200).entity(output).build();

}

private void writeToFile(InputStream uploadedInputStream,
        String uploadedFileLocation) {

    try {
        OutputStream out = new FileOutputStream(new File(
                uploadedFileLocation));
        int read = 0;
        byte[] bytes = new byte[16000];

        out = new FileOutputStream(new File(uploadedFileLocation));
        while ((read = uploadedInputStream.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }
        out.flush();
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    } } } 

ファイル サイズが 10 KB を超える場合に変更する必要がある設定を教えてください。

ありがとう!

4

2 に答える 2

0

ジャージーでも同じ問題が発生しました。ジャージトレースをアクティブにしましたが、何も役に立ちません。

私はapacheライブラリによってライブラリを変更しましたが、Tomcatの一時ファイルのリポジトリにリンクする際の問題よりもわかります。リポジトリは存在しませんでした。10k未満のファイルの場合、リポジトリは使用されませんでした。

したがって、リポジトリの作成後、私はジャージライブラリを使用し、すべて正常に動作します。

于 2012-05-23T12:04:04.633 に答える
0

私はジャージーのコンテキストで org.apache.commons.fileupload.servlet.ServletFileUpload を使用していますが、うまく動作します。はい、最大ファイルサイズを設定しました。

ここに私が使用するコードのスニペットがあります (これはマルチパート フォームなので、ファイルと一緒に他のフィールドがあります)

private LibraryUpload parseLibraryUpload(HttpServletRequest request) {
LibraryUpload libraryUpload;

File libraryZip = null;
String name = null;
String version = null;

ServletFileUpload upload = new ServletFileUpload();
upload.setFileSizeMax(MAX_FILE_SIZE);

FileItemIterator iter;
try {
  iter = upload.getItemIterator(request);
while (iter.hasNext()) {
....
if (item.isFormField()) {
....
}else{
BufferedInputStream buffer = new BufferedInputStream(stream);
      buffer.mark(MAX_FILE_SIZE);
      libraryZip = File.createTempFile("fromUpload", null);
      IOUtils.copy(buffer, new FileOutputStream(libraryZip));
...
}
于 2012-04-23T16:27:04.427 に答える