@MultipartForm 如何获取原始文件名? [英] @MultipartForm How to get the original file name?
问题描述
我正在使用 jboss 的 rest-easy 多部分提供程序来导入文件.我在这里阅读 http://docs.jboss.org/resteasy/docs/1.0.0.GA/userguide/html/Content_Marshalling_Providers.html#multipartform_annotation 关于@MultipartForm 因为我可以将它与我的 POJO 精确映射.
I am using jboss's rest-easy multipart provider for importing a file. I read here http://docs.jboss.org/resteasy/docs/1.0.0.GA/userguide/html/Content_Marshalling_Providers.html#multipartform_annotation regarding @MultipartForm because I can exactly map it with my POJO.
下面是我的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;
}
}
然后我得到了文件对象并打印了它的绝对路径,它返回了一个类型为 file 的文件名.扩展名和上传的文件名丢失.我的客户正在尝试上传存档文件 (zip,tar,z)
Then I got the file object and printed its absolute path and it returned a file name of type file. The extension and uploaded file name are lost. My client is trying to upload a archive file(zip,tar,z)
我在服务器端需要此信息,以便我可以正确应用取消归档程序.
I need this information at the server side so that I can apply the un-archive program properly.
原始文件名在 content-disposition 标头中发送到服务器.
The original file name is sent to the server in content-disposition header.
我如何获得这些信息?或者至少我怎么能说 jboss 用上传的文件名和扩展名保存文件?它可以从我的应用程序配置吗?
How can I get this information? Or atleast how can I say jboss to save the file with the uploaded file name and extension? Is it configurable from my application?
推荐答案
在查看了一些 Resteasy 示例后,包括这个 one,当使用带有 @MultipartForm的 POJO 类时,似乎无法检索原始文件名和扩展名信息代码>注释.
After looking around a bit for Resteasy examples including this one, it seems like there is no way to retrieve the original filename and extension information when using a POJO class with the @MultipartForm
annotation.
到目前为止,我所看到的示例通过 HTTP POST 从提交的多部分表单数据的文件"部分中检索 Content-Disposition
标头中的文件名,本质上类似于:
The examples I have seen so far retrieve the filename from the Content-Disposition
header from the "file" part of the submitted multiparts form data via HTTP POST, which essentially, looks something like:
Content-Disposition: form-data; name="file"; filename="your_file.zip"
Content-Type: application/zip
您必须更新您的文件上传 REST 服务类才能像这样提取此标头:
You will have to update your file upload REST service class to extract this header like this:
@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();
}
希望这会有所帮助.
这篇关于@MultipartForm 如何获取原始文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!