如何发布二进制文件从JQuery的客户端Java服务器使用REST [英] How to Post Binary File From JQuery Client to Java Server using REST

查看:76
本文介绍了如何发布二进制文件从JQuery的客户端Java服务器使用REST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的客户端(jQuery的)的二进制文件发布到我的服务器(JAVA)。我使用的Apache CXF和休息。该文件能够进入服务器,及时抛出异常。

下面是JavaScript的在客户端:

 函数handleFileUpload(){
     的console.log(handleFileUpload称为);
     VAR URL =HTTP:// MYSERVER:8181 /引导/ REST /上传/许可证;
     。var文件= $('#file_upload')得到(0).files [0];
     $阿贾克斯({
        网址:网址,
        类型:后,
        数据:文件,
        过程数据:假的,
        成功:函数(){
           $(#file_upload_result)HTML(提交成功);
        },
        错误:函数(){
          $(#file_upload_result)HTML('有在提交错误');
        }
    });
  }

下面是服务器端的code:

  @POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
@Path(/许可证)
公共字符串uploadLicenseFile(@FormParam(文件)的InputStream pdfStream)
{
    尝试
    {
        //字节[] = pdfByteArray convertInputStreamToByteArrary(pdfStream);
        //文件长度= pdfByteArray.length;
        文件长度= pdfStream.available();
        响应=上传成功!;
        // TODO在内存中读取文件并存储PARAMS
    }
    赶上(异常前)
    {
        响应=上传失败:+ ex.getMessage();
        文件长度= 0;
    }
    返回getFileLength();
}


解决方案

您要发送的文件后的身体,你想要做的就是发送的文件在多部分表单数据的身体。您可以使用FORMDATA对象来做到这一点。

 函数handleFileUpload(){
     的console.log(handleFileUpload称为);
     VAR URL =HTTP:// MYSERVER:8181 /引导/ REST /上传/许可证;
     。var文件= $('#file_upload')得到(0).files [0];
     VAR FORMDATA =新FORMDATA();
     formData.append(文件,文件)
     $阿贾克斯({
        网址:网址,
        类型:后,
        数据:FORMDATA,
        过程数据:假的,
        的contentType:假的,
        成功:函数(){
           $(#file_upload_result)HTML(提交成功);
        },
        错误:函数(){
          $(#file_upload_result)HTML('有在提交错误');
        }
    });
  }

I'm trying to post a binary file from my client (jQuery) to my server (Java). I'm using Apache CXF and REST. The file is making it to the server, which promptly throws an exception.

Here's the JavaScript on the client side:

  function handleFileUpload() {
     console.log("handleFileUpload called");
     var url = "http://myserver:8181/bootstrap/rest/upload/license";
     var file = $('#file_upload').get(0).files[0];
     $.ajax({
        url: url,
        type: "post",
        data: file,
        processData: false,
        success: function(){
           $("#file_upload_result").html('submitted successfully');
        },
        error:function(){
          $("#file_upload_result").html('there was an error while submitting');
        }   
    }); 
  }

Here is the server-side code:

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
@Path("/license")
public String uploadLicenseFile(@FormParam("file") InputStream pdfStream) 
{
    try 
    {
        //byte[] pdfByteArray = convertInputStreamToByteArrary(pdfStream);
        //fileLength = pdfByteArray.length;
        fileLength = pdfStream.available();
        response = "Upload successful!";
        // TODO read file and store params in memory
    } 
    catch (Exception ex) 
    {
        response = "Upload failed: " + ex.getMessage();
        fileLength = 0;
    }
    return getFileLength();
}

解决方案

You are sending the file as the post body, what you want to do is send the file in a multi-part form data body. You can use the FormData object to do this.

  function handleFileUpload() {
     console.log("handleFileUpload called");
     var url = "http://myserver:8181/bootstrap/rest/upload/license";
     var file = $('#file_upload').get(0).files[0];
     var formData = new FormData();
     formData.append('file', file)
     $.ajax({
        url: url,
        type: "post",
        data: formData,
        processData: false,
        contentType: false,
        success: function(){
           $("#file_upload_result").html('submitted successfully');
        },
        error:function(){
          $("#file_upload_result").html('there was an error while submitting');
        }   
    }); 
  }

这篇关于如何发布二进制文件从JQuery的客户端Java服务器使用REST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆