Reputation:
After uploading a file it gets stored as a .tmp file in a temp directory.
How can one get the actual file type of the file that was uploaded? I need to know if it's a bmp, jpeg, png, etc. from server side.
Note: I'm also using Struts 2 version 2.1.8.1
Upvotes: 1
Views: 1386
Reputation: 2317
apart from Truong Ha's answer (MimetypesFileTypeMap needs activation.jar), there are some more other ways as well:
http://www.rgagnon.com/javadetails/java-0487.html
cheers,
Upvotes: 1
Reputation: 10954
Try to touch the mime type of it
File f = new File("temp.tmp");
System.out.println("Mime Type of " + f.getName() + " is " + new MimetypesFileTypeMap().getContentType(f));
the output should be something like: image/gif
or image/png
or image/jpg
etc.
Upvotes: 1
Reputation: 240966
On JSP if you have
<s:form action="fileUpload" method="post" enctype="multipart/form-data" >
<s:file name="userFile" label="File" />
<s:submit />
</s:form>
in your ActionClass you need to add
public void setUserFileContentType(String userImageContentType) {
this.userImageContentType = userImageContentType;
}
public void setUserFileFileName(String userImageFileName) {
this.userImageFileName = userImageFileName;
}
and then you would have access to the fileName and contentType of uploaded file
Upvotes: 0