I don't have the code available right now, but I'll describe my situation and post code later on :). Basically, my problem is with uploading image to server via POST (from Java desktop application / client). I found some discussions here on this matter, but it either included using org.apache.commons.httpclient classes, and I have most of the code implemented using URLConnection class, OR with bugs in it. Check this selected answer to see what I followed in as much detail as possible: Using java.net.URLConnection to fire and handle HTTP requests
Anyways, I created php script on my local server that handles form with file upload. I checked it from one static html that was created along with .php. I then checked it without file upload, and it works ok.
It also works ok when I send plain text file. This looks like a problem related to encoding. Maybe some strange character is breaking my transfer?
As I said, I'll include code if necessary later today, but I'm interested in your opinions / experience related to encoding images when sending via POST. Should I do something else, that's not being mentioned in the link above.
QUESTION: do I need to use some kind of encoding to actual data when sending binary file in multipart message via http post?
Sorry for no code right now.
Cheers
As for your question: yes, you need to encode the file.
I would use HttpClient for this as it takes care of all those things for you. No need to reinvent the wheel. See How to upload a file using Java HttpClient library working with PHP for code.
If you are using URLConnection to handle multipart/form-data manually, I guess you you need to add "Content-Transfer-Encoding: binary" to the image part. See W3C's docs.
Related
I need to convert a web page [which has not public access] to PDF or Image [preferably to PNG].
Web page contains set of charts and image. Most of the charts are populated through Ajax calls so there is a delay between page load and chart load.
I am looking answer for any of these questions:
1- I found set of snapshot api's but none of them support accessing my internal page. Since the web page I am trying to export is not public I need to be authenticated. Biggest problem is I cannot send request headers [such as session-id, cookie or other variables] along with these API's. It seems they don't support this kind of functionality.
2- I am not sure if I can do following: Login to my web page with HTTP client, add http headers, send get call and get HTML string. Then use one of the converters to convert it to PDF. What I am not sure is if it's possible to get proper PDF from the HTML string I got from http client since resources [css, js and etc] will be missing. I want my pdf/image looks exactly as it on the web site.
I really appreciate if you can help.
Thanks in advance,
ED
You're probably best of using wkhtmltopdf, which is a server-side tool and is easily installed.
There are two parameters you can use to wait for your Ajax to finish, try:
javascript-delay to influence the time the program waits for the JavaScript to finish
window-status to wait for a certain return code for the window
See the extensive manual for this program here
wkhtmltopdf generates a PDF and wkhtmltoimg generates an image, which is PNG (as you requested) by default.
Authentication is difficult because it involves security. Because the operation you are describing is unusual it is likely to result in all kinds of alarm bells going off. It is entirely possible to do but it is fraught, easy to get wrong and fragile in the face of security updates and code changes.
As such I'm going to suggest an alternate method which is one we often recommend for ABCpdf (on which I work). Yes we support standard authentication methods but the beauty of this approach is that it is robust and is applicable to other solutions (eg Java based) and novel authentication methods.
Typically you just want a PDF of the current page. The easiest way to do this is snaffle the HTML. The way you do this rather depends on your environment. For example under ASP.NET you can obtain the HTML of the current page using the HttpResponse.Filter property or by overriding the Render method of the page. The way you do it will depend on what you're coding in.
Then you need to save this HTML to a file and present it to your solution via a 'file://' protocol URL. Now obviously at this point any relative links will be broken but this is easily fixed by dropping in a BASE tag that references the place they are located.
Generally the types of resources referenced by an server-side page are static. So if you can create a tag that references the actual files rather than a web site, you will bypass any authentication for access to these resources.
That still leaves the AJAX based problems which are another can of worms. The render delay method is something we have supported for many years (from before AJAX was around) however it is not terribly reliable because you just don't know how long to wait.
Much better is a tighter link into the JavaScript via a callback you can use to determine if the page is loaded. I don't think ABCpdf is going to be appropriate for you since it is .NET but I would certainly encourage you to look for a Java based solution that uses this type of more sophisticated approach.
I have a situation where in I write to a text file programmatically using java and simultaneously I read from the same file using jQuery.
The problem I face is jQuery is unable to find the updated content whenever a content is written into the text file via java.
I have Googled a lot but the only results I find are for java and java processing and not for java and javascript (i.e A Client side and Server side)
I am not sure if this is even possible.
More about the question:
I write into the file the crawling results using java and I am trying to display the same using javascript (jQuery.post() method).
JAVA
A multi-threaded crawling program that crawls a website and does some functionality. I am trying to write some content into a text file using the same java program as and when the crawling happens. The content I write mostly are the details about which thread is getting invoked and what is the current link that is being crawled.
The reason I write this in the text file is I need to show the output in the UI so that people looking at the UI will understand what happens.
Writing happens perfectly as expected.
JAVASCRIPT (jQUERY)
This using the
jQuery.get or post ("sample.txt", function (result) {
$("#someID").html(result);
});
It reads from the text file normally but when java and javascript both are trying to access the file, It is the java that dominates leaving javascript behind thus jQuery is unable to fetch the updated content as and when it happens.
I guess this explanation is more than sufficient to make people understand what exactly my problem is !
On the whole, java and javascript try to access the same file at the same time. So there comes this issue.
Any help is appreciated.
Thanks in advance
I think the file is cached. Easiest thing is to request the file by different urls. Try something like "sample.txt?rnd="+Math.rand()
There can be synchronization problems and your data will be corrupted.
I have a question, is it must be done with Ajax? I think you are trying to figure out about
Ajax push and pull
This is not very easy to do and I wouldn't really recommend it. However, there is a better technology called websocket. So what you can do is, client can submit request to the server to write data into a file then server can send back updated content to the client. Moreover, this is much better than achieving the same objective through numerous amount of HTTP requests.
Additionally, if you want the crossbrowser compatibility, have a look at http://socket.io/
Thanks for all those who were trying to help me out.
I have finally come up with a solution. I, instead of using jquery post to directly read from file, am using another jsp file that reads the file contents and prints using out.println on screen, and after which I am using jQuery post to get the content written by that jsp file. Hence the synchronization problem is avoided.
Here is more about my explanation:
Earlier I had
java program -> Text File <- javascript (jQuery post) // Resulted in synchronization problem where in javascript was not able to access the updated content.
Now
java program -> Text file <- JSP file <- javascript (jQuery post) // Avoided the synchronization problem as that file is accessed by the same server side language. After that jQuery reads the content printed by JSP page.
After many changes, finally came up with one good working solution.
Thanks all.
I need to stream audio from a location that can't be directly accessible to a client app. To this end Im investigating using a Java servlet or PHP to create the stream on an as needed basis.
Anyone done this before?
How would this handle skip / seek commands from the <audio> tag?
Is there any way to prevent a client from caching the audio?
Anyone done this before?
Yes, I'm pretty sure this has been done before. In case you're looking for code, see HTTP_Download.
How would this handle skip / seek commands from the <audio> tag?
There is nothing to do on the server-side about that. Your browser handles that. If you want to handle that in the browser, please consult your browsers technical specification about the features of the <audio> HTML tag your browser supports.
Is there any way to prevent a client from caching the audio?
Not at all. The current copy of the data is available at the client side, otherwise the browser is not able to play / skip / seek the media.
Use this to block caching:
header("Cache-Control: no-cache, must-revalidate");
Someone must have done it to protect copyrighted music..
It would still work fine with skip and seek tags.
If you are generating the audio tag from a script then you can force the sample to reload by adding a dummy GET variable to the end of the sample file name. Something like this...
echo "<source src=\"sample.php?rand=" . rand(1,99999) . "\" />\n";
I know that there is Vuze API and jBitTorrent library but I can't use Vuze because of I have server without GUI and jBitTorrent is too old and too buggy. Can you tell me that bittorrent library for Java is good now? I need to download files through torrent file only. Thanks.
It seems you need to run Vuze headless through it's console ui.
When I tried it with the apt-gettable Vuze on Ubuntu, I didn't need to 'grab a few support jars' as the wiki linked above specified - it just ran straight away.
You can use Transmission or µTorrent, or just run Vuze headless. With sending files to Transmission, you need to encode the binary data as base 64, with sending them to µTorrent, you need to use a multipart form via HTTP POST.
Here are their API pages:
https://trac.transmissionbt.com/browser/trunk/extras/rpc-spec.txt
http://www.utorrent.com/community/developers/webapi
I would like to transfer a file from the Flex front end to a back end Java web service, how can I achieve this ?
Will byte array be a good option for the transfer ?
It would be appreciated if you can give a hint as to how to achieve the solution or point me in the right direction.
Note: the file is a small .jpg file, and I am new to Java
Have a look on http://www.adobe.com/devnet/flex/articles/file_upload.html
You can use Flex "FileReference class" to Upload a file on Server
Flex Working with file upload and download
and commonly on server there should be a servlet to accept multipart request
using
Apache Commons FileUpload
this is useful example of servlet
Servlet File Upload Example
Hopes that helps
I have used a byte array to transfer files when I know they will be small. It can be a lot simpler to post them when dealing with https/cert issues, etc. that FileReference does not work well with. A FileReference upload is your other option (typical solution). Either way you'll use FileReference to select the file and then either use .upload to upload it or .load to load bytes in. Then you'll use .data to get the byte array. If your Jpg is coming from a snapshot taken from a flex component in memory, you'll need to work with a special Jpeg image encoder. I can tell you how to do that if that is what you are doing. Really beyond the scope of your original question, though.