【问题标题】:415 state file to upload with Glassfish 4 and Java EE 7使用 Glassfish 4 和 Java EE 7 上传的 415 状态文件
【发布时间】:2014-07-20 21:13:50
【问题描述】:

下午好,希望你能帮助我。 我正在尝试发送带有 xhr 对象的表单。 我的 html 页面中有这个

<form action="" method="post" enctype="multipart/form-data">
    <p>
        Choose a file : <input type="file" name="archivos"/>
    </p>
    <input type="submit" value="Upload" onclick="sendForm(this.form)"/>
</form>

我对 xhr 对象所做的表单操作,在我的 javascript 文件中如下

function sendForm(form) {
    var formData = new FormData(form);
    var xhr = new XMLHttpRequest();
    xhr.open('POST','http://localhost:8080/project/webresources/generic/archivo',false);
    xhr.send(formData);
}

我的存档休息的前一个网址 休息在我的文件中,我有以下内容:

@POST
@Path("/archivo")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces("application/json")
public void uploadFilebyRest(
        FormDataMultiPart multiPart)
        throws IOException{
    List<FormDataBodyPart> fields = multiPart.getFields("archivos");
    for (FormDataBodyPart field : fields) {
        handleInputStream(field.getValueAs(InputStream.class));
    }
}

然后使用方法 handleInputStream 我收到 InputStream 成就并在我的硬盘上创建文件

private String handleInputStream(InputStream uploadedInputStream) throws IOException {
    byte[] Arraybytes = IOUtils.toByteArray(uploadedInputStream);
    InputStream input = new ByteArrayInputStream(Arraybytes);
    int data = input.read();
    String uploadedFileLocation = "c://Imagenes/Foto.jpg";
    OutputStream out;
    out = new FileOutputStream(new File(uploadedFileLocation));
    while (data != -1) {
        out.write(data);
        data = input.read();
    }
    out.flush();
    out.close();
    input.close();
    return "";

}   

以上所有代码都适用于我使用 java EE 6 和 Glassfish 3 有人告诉我为什么我在使用 Java EE 7 和 Glassfish 4 时无法工作 我的日志中有以下内容

415 不支持的媒体类型帮助我!谢谢。

【问题讨论】:

    标签: rest file-upload glassfish java-ee-7 http-status-code-415


    【解决方案1】:

    Glassfish 4 包含 Jersey 2.x,其工作方式与 1.x 分支不同。为了让 multipart 工作,您需要在您的 jax-rs ApplicationResourceConfig 类中注册 MultiPartFeature

    https://jersey.java.net/documentation/latest/media.html#multipart

    不过,请注意。 Glassfish 4 中有一个 Jersey/CDI 错误,当您部署使用 MultiPartFeature 的应用程序时会导致几个异常。该错误已在以 b08 开头的 Glassfish 推广版本中修复。

    【讨论】:

      猜你喜欢
      • 2018-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-28
      • 1970-01-01
      • 2015-02-11
      • 2014-11-14
      • 2016-07-06
      相关资源
      最近更新 更多