I have this particular piece of code for restricting the users to upload image files only.
if (!fileName.getContentType().startsWith("image/"))
errors.add("", new ActionError("errors.imageFile.contentType"));
Similary I want the users to upload only files with extension ".txt" in another scenario. What MIME type should I use or please let me know the code which will be helpful for achieving this task.
Typically the mime type for text files is text/plain
Text files have the following MIME type:
text/plain
However, according to this site, it is not the only one. You can use Apache's FileNameUtils getExtension method to get the extension of the file.
What MIME type should I use ..?
Content-Type: text/plain
I want the users to upload only files with extension ".txt" in another scenario.
The mime type for plain text files is "text/plain". Or you can check the name of the uploaded file.
However, these won't prevent users uploading non-text files. All they need to do (on Windows) is to rename a non-text file to have the ".txt" extension ... and then upload it.
If you really want to make sure that users only upload text, you need to test the files after they have been uploaded.
Related
I am working on some project that requires changing attachments of an Outlook .msg file.
I chosen to use Apache POI with its low-level POIFS API.
I am able to substitute binary data of an attachments by re-writing PR_ATTACH_DATA_OBJ ByteChunk, as well as few other Chunks related to filename, extension, displayName.
However, when I open the resulting .msg file in outlook my attachments viewed as no-extension file but content is exact what I have pushed to it:
There are two more fields that I can't find possible data to write as well as there is no description for them in MAPIProperty.Java POI dictionary:
Can somebody advice me how I can make attachment substitution possible to success? What kind of data I have to put into these two fields in order to fix preview? Will this help?
I am looking for a free solution. I already tried Aspose.Email and this works brilliant but it is not currently an option.
Embedded message attachment (stored in PR_ATTACH_DATA_OBJ property on an attachment) is not a file.
Does Outlook correctly open the attachment? Make sure the attachment type remains the same.
I want to get the extensions of a few files from their download links.
Download links does not contain the extensions of their files. For example, a link looks like below:
http://yourshot.nationalgeographic.com/u/fQYSUbVfts-T7odkrFJckdiFeHvab0GWOfzhj7tYdC0uglagsDNfNYI4FFesWV5zeSPtcfpyHzKZI7dHjkluwtIYNkXOGmjh43Ktdn0VeBWhQ-9l2kheOPt5N2TM3yPEW4tTrtFFqniatwxxhbqsc78IU2pBaqWwyEVLeQx64zSda2CNGmUpSxyte_tamVoIk3y4zXisQ-vjmMp6n1BAB3nbUVlwWg/
I tried to get the files extension using myHttpUrlConnection.getContentType(), but the result was not the result what I want.
Some download links return a phrase like “text/plain”, ”application-octet-stream”,multipart/form-data ,…. But I just want correct and clear type, like rar, mp4, txt, jpeg,mkv, zip, png, apk, mp3, … .
You cannot do that. The getContentType() method simpy:
Returns the value of the content-type header field.
which in most cases is (though there is no guarantee) related to the file extension/file type, for example application/pdf would mean there is a PDF file under that URL.
Each of the file types with extension you have listed (rar, mp4, txt, jpeg,mkv, zip, png, apk, mp3) have another structure. To do reliably what you want to do, you would have to first download the whole file and then check its type based on the contents.
A good example of a library you could use is Apache Tika.
So I am using S3 to store basically audio files, I stream those files with cloudfront. I need to modify the metadata of those files (not the metadata of the s3 object, but the tags of the music), or convert those files to another format (mp3 to m4a, etc). So the way I see it I need to download these files to my server, modify the files or transcode this files, and reupload the files.
I see some ways to do this but I have some doubts on which is the correct way or best way to do it.
So one way would be to download the file with the following code
S3Object object = s3.getObject(new GetObjectRequest(bucketName, key));
System.out.println("Content-Type: " + object.getObjectMetadata());
//displayTextInputStream(object.getObjectContent());
And I could use File to write the file to my server.
My question heres is how do I obtain just the file name from the S3Object, I was looking in the metadata and tried to use getContentDisposition(), but it returns null, looking directly in AWS Console I see the proper name of the file without the path.
The other idea I have is to use cloudfront to download the file, creating a download distribution.
Can I work directly with and inputstream to modify the metadata?
You can save this file under any name you choose, just like any other file you download through HTTP.
However, as you mentioned in your question:
String disposition = object.getObjectMetadata().getContentDisposition()
Should give you the optional Content-Disposition HTTP header, which specifies presentation information for the object such as the recommended file-name for the object to be saved as.
This function returns null if the Content-Disposition header hasn't been set.
This is from Amazon online documentation: For more information on how the Content-Disposition header affects HTTP client behavior, see http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html#sec19.5.1.
My server is implemented with few servlets when each one is responsible for different task.
I need to make client to download a specified xml file from server when a SAVE button in html page pressed.
I've read that the best way is to host file on server and just let client download, but I don't know how to implement it.
Any example will be highly appreciated. :)
p.s.
I'm using JAVA.
Do these steps:
Set proper MIME-type for your file. If you do not want (there's no suitable MIME-type) to set MIME-type specific to your file type, then set it to application/octet-stream
Set content length to the response
Set content disposition
Open output binary stream, then read your file and write its contents to this output stream.
That's it.
Here is the sample code
Suppose users can upload files, I want to find the extension of the uploaded file.
Even if the user has renamed the extension of the file, I want to find the real extension of that file by it's header or bytecode..etc.
Please help me with a solution.
Note: Not just the extension by substring() or getContentType() but the real file extension,
say for example(in windows), its a .doc file and user renames it to .jpg and uploads it.
Its possible in php, but I don't know how to do that in java! but it can be done.
Thank you.
Apache Commons: FileUpload is a good place to start. further, you could look at the link i referenced: JSP: Get MIME Type on File Upload for hints on how to do this.
As the user states in their question, the thought is if i rename a .png to a .jpg this will fool the getContentType() into thinking it's now a .jpg file. A quick search on google provided the following answer result: Get the mime type from a file that lists 3 very good options:
Apache Tika
The Apache Tika™ toolkit detects and extracts metadata and structured text content from various documents using existing parser libraries
JMimeMagic
jMimeMagic is a Java library for determining the MIME type of files or streams
A java library that claims to help you with this, is: mime-util
Enable Java programs to detect MIME types based on file extensions, magic data and content sniffing. Supports detection from java.io.File, java.io.InputStream, java.net.URL and byte arrays.
A quick search brought this post up: http://fredeaker.blogspot.com/2006/12/file-type-mime-detection.html
The post lists several "magic" libraries that can detect the file type based on its contents.