I'm using Play Framework 2.0.3 to create an application which delivers Excel files that should be downloadable by the user.
response().setContentType("application/x-download");
response().setHeader("Content-disposition","attachment; filename=tradeLogTest.xlsx");
but,how to get the outputstream from response()?tks
Play's action can return a File:
response().setContentType("application/x-download");
response().setHeader("Content-disposition","attachment; filename=tradeLogTest.xlsx");
return ok(new File("/absolute/path/to/tradeLogTest.xlsx"));
Here's an API for Results
Providing download option for static files can be done in Play as:
Ok.sendFile(new File("path to file/abc.csv"), inline=true).withHeaders(CACHE_CONTROL->"max-age=3600",CONTENT_DISPOSITION->"attachment; filename=abc.csv", CONTENT_TYPE->"application/x-download");
There are other parameters that are also available
For Internet Explorer - make sure you set the Content Disposition
Serving files
If it’s not a problem to load the whole content into memory for simple content what about a large data set? Let’s say we want to send back a large file to the web client.
read more at : http://www.playframework.com/documentation/2.0.x/JavaStream
Related
There is a betting exchange website which offer their data in XML from the following link:
http://odds.smarkets.com/oddsfeed.xml
I would like to access this link to retrieve the latest data (in java). Previously I have had to download the (very large) file and add it to my project and get the data from there. What is the best way to achieve this without having to download the file every time I want to access the data?
I plan on storing the returned data into a database.
Thanks
Well this seems to be very tricky question .I would suggest you to create a simple web service application[Client/server architecture] to get the contents from this url. You can use REST to call this url. But what contents you need to read depends on the functionality that you want to achieve.You need to write your custom logic to read the data.Here in you will be acting as client and the url would be your service.
You can refer following link
https://community.atlassian.com/t5/Confluence-questions/Access-page-content-via-URL/qaq-p/163060
It is quite a common question but I can't find an answer to it
I have a simple HTML with an input text box (type=file) and a submit button. On clicking the submit button, I call a js function where I try to get the complete path of the file
var data = $('#fileName').val();
the issue is I am not getting complete file path of the file I am uploading. I know due to security reasons chrome gives me a C:\fakePath\filename and firefox gives me only the fileName. But in case I need a complete path what shall I do?
PS: Further I will make an ajax call and give that file path to the back-end which needs it to read that file using FileReader
You cannot get the complete path! there is no way to do that!! Even though you are on an intranet and you have enough permissions.
A workaround for this is to have a textarea and ask the user to enter the complete path of the file.
In short you can't have the full name of a file once is loaded on server side, you will just have the file name and its content in a raw byte array (among other attributes). This is not a Java thing nor other server side technologies issue, is related to browser implementation (but it looks that IE6 may contain a flaw about this).
Not directly related to your question but caught my attention
PS: Further I will make an ajax call and give that file path to the back-end which needs it to read that file using FileReader
Usually, you can't handle a file upload using ajax because it can lead to security holes. Still, there are some browsers (like Chrome and Firefox) that allows you to send a file using XMLHttpRequest but that isn't allowed on some browsers (like IE8-) so you have to use an iframe in order to make the file ajax uploading work.
In order to avoid handling all these problems, I would advice you to use a third-party js library that handles the ajax file upload. An example is blueimp jQuery file upload that also has Java server side examples (DISCLAIMER: I do not work in this project nor I'm associated with blueimp in any way). Note that using this plugin requires that you have a mid knowledge on HTML/JavaScript/jQuery/Java Server Side so if you're a starter it may take you some time to make it work, but once it does is pretty good.
I dont know which technology you are using.. but you can always get file name once it is uploaded on server (Using php or .net )
your steps to upload should be like below:
1) Upload file to the server (e.z. /uploadedFiles/...filename
2) Create a method which will fetch file name from the uploaded path
3) simply insert file name in to the database (this will give you flexibility to change folder name of uploaded docs in future if required)
Generally filenames are not stored as it is . to avoid name conflict in future. So it is a advisable to always rename your filename by adding minutes & seconds after itsname.
If any doubts do ask.
Hope it helps.
Browsers block the filepath access on javascript for securit reasons.
The behavior makes sense, because the server doesn't have to know where the user stores the file on his computer, it is irrelevant to the upload process.
Is it possible to upload a file in Play! framework only by giving a path in url?
For example I would like to call:
www.mywebsite.com/upload_PATH
It's for me quite important, because I would like to upload and process a lot of data. Selecting manually 1000 files is too much time consuming and I want to write a program which will make it for me :-) I'm using Play with Java.
If upload_PATH is a local file path on your system, it is not a good way to go.
You should write a Play action where you can upload a file, as it is done in this example.
Then, you should write a HTTP client (started by a main Java method) which go through your files and upload then calling the Play! action. You can use the httpclient Apache library for writing the client part.
As nico_ekito wrote www.mywebsite.com/upload_from_local_path won't work
In case of huge amount of files you can create temporary folder on the distant server and upload your files with FTP. Then in your app you'll need only action for post-upload processing, ie. it can check if file is valid and move it to calculated destination and register in database if required.
Other possibility is using some flash/ajax multi uploader for an example swfupload (don't know it, it's just first hit from the search engine). This approach will be better if you are going give the upload possibility to people who you don't want to give any FTP access.
Finally you can mix the solutions -> use uploader instead of FTP and later post-process new items remotely.
I'm trying to build a simple HTTP Server using Java, using
java.net.ServerSocket = new ServerSocket(this.port, 0, this.ip);
java.net.Socket connection = null;
connection = server.accept();
java.io.OutputStream out = new BufferedOutputStream(connection.getOutputStream());
when connected using web browser, i'm simply write the output (HTTP headers + html code) from a string
String headers = "http headers";
String response = "this is the response";
out.write(headers.getBytes());
out.write(response.getBytes());
out.flush();
connection.close();
and the browser display it correctly.
And now my problem is, i want to construct a full webpage (html, javascript, css, images) and put those files into the Java package (JAR) file, and of course, those files are designed not-to-be modified after the JAR is ready to use. And here's the questions:
how to achieve this? storing the files inside the JAR and then output them when a connection is made.
how output images file (non-text) just like output-ing String by out.write() ?
Thanks, any sample or code is appreciated.
Is implementing an HTTP server your primary problem or just a way to achieve some other goal? If the latter, consider embedding Tomcat or Jetty, much simpler and with standard servlet API.
Back to your question: JAR is just a ZIP file, you can put anything there, including files, images, movies, etc. If you place a file inside a JAR file you can load it easily with:
InputStream is = getClass().getResourceAsStream("/dir/file.png");
See these questions for details how getResourceAsStream() works:
Junit + getResourceAsStream Returning Null
getResourceAsStream() vs FileInputStream
how do you make getResourceAsStream work while debugging Java in Eclipse?
Different ways of loading a file as an InputStream
getResourceAsStream() is always returning null
About your second question: when you have an InputStream instance you can just read it byte-by-byte and copy to target out OutputStream. Of course there are better, safer and faster ways, but that's beyond the scope of this question. Just have a look at IOUtils.copy():
IOUtils.copy(is, out);
And the last hint concerning your code: if you are sending Strings , consider OutputStreamWriter and PrintWriter which have easier API.
To work with JAR files use JarOutputStream or ZipOutputStream. To output binary data just do not wrap your output stream with Writer. OuputStream knows to write bytes using method write(byte) and write(byte[]).
The only question here is "Why are you developing HTTP server yourself?"
As long as it is not a housework I would not try to reinvent the wheel and develop another web server. There a small embedded Java web-servers available which can be used for that purpose.
I have for example use the Tiny Java Web Server and Servlet Container several times.
If you have integrated it into your application you can implement a new javax.servlet.http.HttpServlet that reads the files from the resources of your JAR file. The content can be loaded as Tomasz Nurkiewicz already pointed out getClass().getRourceAsStream(...).
Posting this one for a friend. They have an Icefaces app that uses Icefaces's inputfile functionality but it attempts to upload the file to a temporary directory before it allows access to it. Long story short, there is no access to the temp location so copying the file (which will enventually end up in a database) is not possible. Is it possible to use a Java Servlet instead to upload the file and stream the contents to where they do have access without first having the file saved to a temporary location?
Yes, that's absolutely possible. The servlet's doPost() method can do whatever it wants with the input, and is designed for processing it in a streaming manner. However, wiht a bare servlet you'd have to parse the request body manually. Fortunately, Apache Commons FileUpload can do that for you.
Since it was tagged with iceFaces, I am assuming that's what your friend is using in developing this. If that's the case you can use the inputFile component.
Here is a tutorial on how to do it. You can also specify absolute path. It basically uses Commons File Upload under the hood.