2014-10-13 81 views
9

我正在使用jboss的rest-easy多部分提供程序来导入文件。我在这里阅读http://docs.jboss.org/resteasy/docs/1.0.0.GA/userguide/html/Content_Marshalling_Providers.html#multipartform_annotation关于@MultipartForm,因为我可以将它准确映射到我的POJO。@MultipartForm如何获取原始文件名?

下面是我的POJO

public class SoftwarePackageForm { 

    @FormParam("softwarePackage") 
    private File file; 

    private String contentDisposition; 

    public File getFile() { 
     return file; 
    } 

    public void setFile(File file) { 
     this.file = file; 
    } 

    public String getContentDisposition() { 
     return contentDisposition; 
    } 

    public void setContentDisposition(String contentDisposition) { 
     this.contentDisposition = contentDisposition; 
    } 
} 

然后我得到的文件对象并打印其绝对路径和它返回类型的文件的文件名。扩展名和上传的文件名会丢失。我的客户正尝试上传档案文件(zip,tar,z)

我在服务器端需要这些信息,以便我可以正确应用解压缩程序。

原始文件名在内容处置标题中发送到服务器。

我怎样才能得到这些信息?或者至少我怎么说jboss来保存上传的文件名和扩展名的文件?它可以从我的应用程序配置吗?

+0

你可以尝试在你的''文件'中添加'@PartType(“application/zip”)'并查看它是否有效?完整的包是'org.jboss.resteasy.annotations.providers.multipart.PartType'。 –

+0

@isim是的,我会尝试。 tar和Z文件的价值是什么? –

+0

@isim没有用:(它没有用,我不明白为什么人们在没有提供原始apis提供的所有功能的时候包装apis,他们说某些事情可以做得更好,他们提供apis,他们会介绍更多问题,当我们知道这一点时,我们就被锁定了,我从这里开始工作了将近3天,并感到厌倦 –

回答

10

在环视了一下Resteasy的例子,包括这个one后,似乎没有办法检索使用带有@MultipartForm批注的POJO类时的原始文件名和扩展信息。

我已经看到了这么远提交的multipart的“文件”部分检索来自Content-Disposition头文件名中的例子形成通过HTTP POST,基本上,看起来像数据:

Content-Disposition: form-data; name="file"; filename="your_file.zip" 
Content-Type: application/zip 

你会必须更新您的文件上传REST服务类来提取这样的头像这样:

@POST 
@Path("/upload") 
@Consumes("multipart/form-data") 
public Response uploadFile(MultipartFormDataInput input) { 

    String fileName = ""; 
    Map<String, List<InputPart>> formParts = input.getFormDataMap(); 

    List<InputPart> inPart = formParts.get("file"); // "file" should match the name attribute of your HTML file input 
    for (InputPart inputPart : inPart) { 
    try { 
     // Retrieve headers, read the Content-Disposition header to obtain the original name of the file 
     MultivaluedMap<String, String> headers = inputPart.getHeaders(); 
     String[] contentDispositionHeader = headers.getFirst("Content-Disposition").split(";"); 
     for (String name : contentDispositionHeader) { 
     if ((name.trim().startsWith("filename"))) { 
      String[] tmp = name.split("="); 
      fileName = tmp[1].trim().replaceAll("\"","");   
     } 
     } 

     // Handle the body of that part with an InputStream 
     InputStream istream = inputPart.getBody(InputStream.class,null); 

     /* ..etc.. */ 
     } 
    catch (IOException e) { 
     e.printStackTrace(); 
    } 
    } 

    String msgOutput = "Successfully uploaded file " + filename; 
    return Response.status(200).entity(msgOutput).build(); 
} 

希望这有助于。

+0

是的,我已经回到了这个方法,我在他们的论坛上提出了一个问题,在这里https ://issues.jboss.org/browse/RESTEASY-1115。感谢您的努力。 –

+0

听起来不错。你会考虑将这个答案标记为接受吗? :) 谢谢。 –

2

您可以使用@PartFilename,但不幸的是,目前这仅用于书写表格,而不用于阅读表格:RESTEASY-1069

直到此问题已解决,您可以使用MultipartFormDataInput作为您的资源方法的参数。

0

Isim似乎是对的,但有一个解决方法。

在表单中创建一个隐藏字段,并使用所选文件的名称更新其值。提交表单时,文件名将作为@FormParam提交。

以下是您可能需要的一些代码(需要使用jquery)。

<input id="the-file" type="file" name="file"> 
<input id="the-filename" type="hidden" name="filename"> 

<script> 
$('#the-file').on('change', function(e) { 
    var filename = $(this).val(); 
    var lastIndex = filename.lastIndexOf('\\'); 
    if (lastIndex < 0) { 
     lastIndex = filename.lastIndexOf('/'); 
    } 
    if (lastIndex >= 0) { 
     filename = filename.substring(lastIndex + 1); 
    } 
    $('#the-filename').val(filename); 
}); 
</script> 
相关问题