Parsing multi-form request (iframe) - java

I have a httpservice on my android device. So
I send a single file via XMLHttpRequest, if possible, or iframe in every other case.
If I use Google Chrome it works fine and I have no questions, because it uses XMLHttpRequest to send file.
But when I use IE or Opera which send file via iframe I get a problem, because I receive request like below:
------------Gg3pxYwLKOqlVLkQwq3Hyn
Content-Disposition: form-data; name="test"; filename="Android.png"
Content-Type: image/png
‰PNG
some symbols ....
.......................
------------Gg3pxYwLKOqlVLkQwq3Hyn
I want to parse it and take filename and file content as binary or base64 without header and footer. Can I parse it by Android's instruments or should use 3rd part library? Please, help.

I've found solution: http://commons.apache.org/fileupload/

Related

Java/Javascript read content-disposition file content

For my web site, I need to get some data from an URL whose response headers contains Content-Disposition attribute which forces me to download the file. I would like to know how I can read the content of the file without downloading the file to disk and do I/O to read it.
Doing so in either Java or JavaScript would be fine.
Content-Disposition is just advisory. If you use a non-browser client (Java, curl, wget...) and do a GET request, you can just do whatever you want.
(I guess this means your question isn't sufficiently specific)

How to implement file download with Spring MVC in Java?

There is a lot upload examples, but I couldn't find any for download. I'm using Spring 3.0 and I need to generate some text buffer and then let user download it as a file. (There is no static resource.)
So far I've tried to use #ResponseBody and writing to response output stream. In both cases browser display content of the 'file' instead of showing Save dialog box. How do I make browser display a dialog box?
Thanks.
You have to set the Content-Disposition header at your action (and not only the response body):
Content-Disposition: attachment; filename=your-file.pdf

Download generated file in Java with JavaScript/jQuery

I am developing web portal, that is based on Java, JavaScript and jQuery. JavaScript communicates with Java services through JSON. I need to do this: User want to download file based on the date he choose. So I need to create hyperlink, on which user can click and the file is generated. I already have the date and the file (as a byte stream) on Java side, but I don't know how to pass this byte stream as a file to JavaScript side.
If I understand this correctly - you have a server process that can talk JSON to the client and vice versa.
Now the client requests a file and your return a url to the file
I suggest that url to
a) have the extension of the file type and
b) be served with the headers
Disposition: attachment; filename=filename.ext;
Content-type: application/octet-stream
where ext is the extension
So return for example the link
/filefolder/servefile.jsp?file=page.pdf
and have
Disposition: attachment; filename=page.pdf;
Content-type: application/octet-stream
in the server process

How to prompt a streamed file for downloading in Java / Tomcat

I'm creating a java web application runing on a Tomcat server.
One of the functions fill in a StringBuffer variable with data.
At the end, I would like to propose the user to download the generated content packaged in a text file. This without having to store the file on the server.
I've been searching for a code snippet but couldn't find anything corresponding ...
I hope I've been clear enough on my problem.
Thanks in advance,
See Making A Download Servlet
Don't forget to add the servlet to your web.xml.
You have to send a content-type along with the response, so that the browser knows what to do with the body of the response.
Normal text has the content-type text/plain, html is text/html. Images are image/gif and so on. For an unknown mime type you normally set "application/octet", which afaik every browser treats as a download. But I recommend to use the propery content type, so the browser might start a matching application to handle the content (e.g. Office for Documents or XML Editor for XML Files ..)
To send a filename along, which the browser suggests for saving, use the following header (example):
Content-Disposition: attachment; filename="downloaded.pdf"
For sending custom headers, use the setHeader() method in the response object.

How do you initiate/force a download for files that reside externally in PHP or other language?

If you have images or other files that reside externally, how do force the browser to download the link when a user click on it?
The use of "Content-disposition: attachment;" header would do that, but it is not working for files that resides externally without reading and importing the file locally.
You will have to load the resource on the server first. You might want to do some caching also:
<?php
header("Content-disposition: attachment; filename=myfile.jpg");
echo file_get_contents("http://host.tld/path/to/myfile.jpg");
?>
This is not possible. You cannot dictate a client how to handle a different resource than the currently requested one.
You could only use a proxy to fetch the external external file and pass it to the client.
I don't think it is possible to force a file download if you are not controlling the HTTP headers. Content-disposition: attachment is the only way I know of to accomplish this.
Though this is probably not going to work, my only guess would be trying to combine Content-disposition with a Location header:
Content-disposition: attachment; filename=myfile.jpg
Location: http://www.somesite.com/myfile.jpg
(it's a long shot, probably invalid and/or just bad practice)
I use a combination of the aforementioned "Content-Disposition" header, as well as forcing the type:
header("Content-type: attachment/octet-stream");
header('Content-disposition: attachment; filename="'.$filename.'"');
I use a method similar to this for downloading mp4 files, could work for text files:
$file=fopen('http://example.com/example.txt','r');
header("Content-Type:text/plain");
header("Content-Disposition: attachment; filename="example.txt");
fpassthru($file);
You can use the download attribute. Just add download = 'filename.extension' to the download link:
<a link='mysite.com/sfsf.extension' download='filenameuwant.extension'></a>

Categories

Resources