Mysterious FileNotFoundException - java

I´m writing an Android app that communicates with a Tomcat Server running servlets. I´m doing a POST to the Tomcat server, specifying what file I want the server to send back to the client (the Android app). When I try to open this file by adding the file name sent with the POST to the catalog name I get a FileNotFoundException. But hardcoding exactly the same file name works just fine.
Copy pasting the file path obtained when the FileNotFoundException is thrown from the Tomcat servers console into windows explorer opens the correct file. I also wrote both the hardcoded file path, and the one where I append the file name to the catalog path, to a log file. In this log file both texts look exactly the same, and copy pasting both of them into windows explorer opens the correct file. Does anybody have an idea why this yields a FileNotFoundException?
Doing the post from client side
String potentialIP = "http://"+url+":8080/se.myPage/myServlet";
URL url = new URL(potentialIP);
conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setChunkedStreamingMode(0);
// Starts the query
conn.connect();
OutputStream out = new BufferedOutputStream(conn.getOutputStream());
byte[] byteArray = korning.getBytes();
out.write(byteArray);
out.flush();
And the doPost method on the server side looks like this
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
InputStream in = request.getInputStream();
java.util.Scanner s = new java.util.Scanner(in).useDelimiter("\\A");
String fileName = s.hasNext() ? s.next() : "";
String filePath = "C:/myCatalog/"+fileName;
PrintWriter out = response.getWriter();
PrintWriter writer = new PrintWriter("C:/myCatalog/Servletlog.txt", "UTF-8");
writer.write("'"+"C:/myCatalog/hardCodedFileName.txt"+"'");
writer.write("'"+filePath+"'");
writer.flush();
writer.close();
BufferedReader reader = new BufferedReader(new FileReader(filePath));
String svar = reader.readLine();
reader.close();
out.close();
}
Anybody got an idea on how to solve this?

For me it turns out that the server was blocking whatever the default user agent is (maybe it was nothing) and was returning 403. And this throws the FileNotFoundException on my try. I then changed to an example json from another server: http://date.jsontest.com/
This will return a json that looks like this:
{
"time": "05:58:38 AM",
"milliseconds_since_epoch": 1400047118645,
"date": "05-14-2014"
}
By switching to another Server everything worked great - later I figured out that there is a problem with (as I said above) the user agent - set it to something like the Chrome user agent and it also should work normally.
Hope it will help you.

Related

HTTP Get on Android Emulator

Today I started developing a new Android app which is based on the Windows Azure Mobile Services.
I've tried to use the SDK provided by Microsoft for the whole day, without any success. Every time the app performs a web request, this one returns a 400, BAD REQUEST code.
I go to a lower level, with the following code.
URL url = new URL("https://xxxx.azure-mobile.net/api/contents"); //host obscured
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setDoInput(true);
//I obscure the authentication and app key, but they are correct in the program
conn.addRequestProperty("AUTHORIZATION", "Basic xxxxxxxxxxxxxxx");
conn.addRequestProperty("X-ZUMO-APPLICATION", "xxxxxxxxxxxxxxxx");
conn.addRequestProperty("ACCEPT", "application/json");
//connect
conn.connect();
int code = conn.getResponseCode(); //400 in the emulator, 200 in a standard java code
String message = conn.getResponseMessage();
InputStream is = conn.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String read = null;
StringBuilder sb = new StringBuilder();
do {
//Read the content
read = br.readLine();
sb.append(read);
} while(read != null);
//print the content
System.out.println(sb.toString());
//Close the buffers
br.close();
isr.close();
is.close();
System.out.println("Status Code: "+code);
System.out.println(message);
conn.disconnect();
It works perfectly in a standard java application, but in the emulator I still get 400 BAD REQUEST. I really do not understand why!
Maybe an issue of the emulator?
I was running an Android Emulator with API 10.
I'd like to try the code on a real device, but the mac does not recognize it... And the device worked until yesterday since I debugged an application with it.
If you know what I am doing wrong, please help me.
EDIT 20-03-2014:
I made some other attempts. I tried with the HttpClient API. No luck.
I'm still getting 400 as response code and Bad Request as response Message.
The thrown Exception is UnknownHostException... But the InetAddress.getByName(); successfully get an IP address for my web service.
The app still work with Android version greater than 4, I have not tried with Android 3.
I'm very sad and frustrated...
Thank you for your time,
Rik.
SOLVED
Oddly, the problem was in the AUTHENTICATION header.
I computed the header with the following code:
public setHeader(String code, String username)
{
StringBuilder sb = new StringBuilder(username);
sb.append(":");
sb.append(code);
sb = new StringBuilder(Base64.encodeToString(sb.toString().getBytes(), Base64.DEFAULT));
sb.insert(0, "Basic ");
auth = sb.toString();
}
The Base64.encodeToString() method adds a \n at the end of the line. .__.
By adding a trim() in the last line, I threw away the new line and everything went fine in both 2.3.3 and 4 Android versions.

How Do You Upload a File to a Server With PHP using Java [duplicate]

This question already has answers here:
How to send PUT, DELETE HTTP request in HttpURLConnection?
(8 answers)
Closed 9 years ago.
I have a Java applet that needs to upload a file from the user's computer to the server.
I am unable to add other libraries (such as com.apache). Is there a low level way of doing so.
Currently, I have a php file on the server which contains:
//Sets the target path for the upload.
$target_path = "spelling/";
$_FILES = $_POST;
var_dump($_FILES);
move_uploaded_file($_FILES["tmp_name"], $target_path . $_FILES["name"]);
?>
Currently my Java program is sending parameters via POST to this php file.
It sends these parameters by POST using the following code:
try {
//Creates a new URL containing the php file that writes files on the ec2.
url = new URL(WEB_ADDRESS + phpFile);
//Opens this connection and allows for the connection to output.
connection = url.openConnection();
connection.setDoOutput(true);
//Creates a new streamwriter based on the output stream of the connection.
OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
//Writes the parameters to the php file on the ec2 server.
wr.write(data);
wr.flush();
//Gets the response from the server.
//Creates a buffered input reader from the input stream of the connection.
BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
//Loops through and reads the response. Loops until reaches null line.
while ((line = rd.readLine()) != null) {
//Prints to console.
System.out.println(line);
}
//Closes reader and writer.
wr.close();
rd.close();
} catch (Exception e) {
e.printStackTrace();
}
This works for POST'ing data but when I try to send a file using this method, nothing happens (no response from the server nor is the file uploaded).
If anyone has any hints I would be grateful :)
Are you using java.net.URLConnection?
You may want to get some help on this page:
http://www.codejava.net/java-se/networking/upload-files-by-sending-multipart-request-programmatically
Here is the main part:
boundary = "===" + System.currentTimeMillis() + "===";
URL url = new URL(requestURL);
httpConn = (HttpURLConnection) url.openConnection();
httpConn.setDoOutput(true); // indicates POST method
httpConn.setDoInput(true);
httpConn.setRequestProperty("Content-Type",
"multipart/form-data; boundary=" + boundary);
outputStream = httpConn.getOutputStream();
writer = new PrintWriter(new OutputStreamWriter(outputStream, charset),
true);
But, you will need to have the php script be on the same server where your applet came from.

Curl Object to URL Generation

I have webservice that I am trying to call:
The following Curl command works for that
curl -F fa=c.apiupload -F sessiontoken=EA3237F922644115A0F7DB75D0AE388F -F destfolderid=52482BD488DB4AD6887C5C7BF47BD6FC -F filedata=#/Users/cpinera/tmp/panda2.jpg -F zip_extract=1 -F metadata=1 -F meta_img_description="This is a very nice panda" -F meta_img_keywords="panda,happy panda" http://domain.com/razuna/raz1/dam/index.cfm
but the equivalent URL that I generates as my HttpURLConnection for accessing RESTFul API does not :
http://domain.com/razuna/raz1/dam/index.cfm?fa=c.apiupload&sessiontoken=F46D2226463C4ADE866819AACD7D2F5E&filedata=C:\JobInterview\BatchUpload\auth.xml&destfolderid=52482BD488DB4AD6887C5C7BF47BD6FC&zip_extract=1
I get this response for the REST Request:
The content was :: <?xml version="1.0" encoding="UTF-8"?><Response><responsecode>1</responsecode><message>Upload failed This was not an uploaded form type</message></Response>
Here is the Java Code
Method that uses queries the URL:
public static String doQuery(String loginUrl) throws IOException{
URL url = new URL(loginUrl);
HttpURLConnection conn =
(HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
if (conn.getResponseCode() != 200) {
throw new IOException(conn.getResponseMessage());
}
InputStream is = conn.getInputStream();
//Buffer the result into a string
BufferedReader rd = new BufferedReader(
new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
while ((line = rd.readLine()) != null) {
line = StringEscapeUtils.unescapeHtml4(line);
sb.append(line);
}
rd.close();
conn.disconnect();
System.out.println("The content was :: " + sb.toString());
return sb.toString();
}
Method that should upload:
public static void testUpload(String seesionToken, String file ) throws IOException{
String upload = "http://domain.com:8080/razuna/raz1/dam/index.cfm?fa=c.apiupload&sessiontoken="+seesionToken+"&filedata="+file+"&destfolderid=52482BD488DB4AD6887C5C7BF47BD6FC&zip_extract=1" ;
System.out.println(upload);
Authenticate.doQuery(upload);
}
After adding in doQuery:
conn.setRequestProperty("Content-Type", "multipart/form-data");
And changing RESTFul URL to:
http://domain.com:8080/razuna/raz1/dam/index.cfm?fa=c.apiupload&sessiontoken="+seesionToken+"&filedata="+files.toURI()+"&destfolderid=52482BD488DB4AD6887C5C7BF47BD6FC&zip_extract=1"
It seems to detect that URL is trying to send a File but still cannot upload file:
The content was :: <?xml version="1.0" encoding="UTF-8"?><Response><responsecode>1</responsecode><message>Upload failed There was no appropriate FILE found in the upload</message></Response>
Your curl command is uploading the file #/Users/cpinera/tmp/panda2.jpg. Using the URL by itself will not, it will simply access the page, hence your error that 'this was not an uploaded form'.
When you run curl with -F parameters, it sends an HTTP POST request with the specified name-value pairs passed in the body of the HTTP request. When you access a URL using query string parameters, an HTTP GET is performed. It seems likely that the server handles those two request methods differently.
Update, now that you've posted some Java code:
The values in the query string portion of the URL are used to convey information to the web server that will be handling the request. Of course, this remote server is not going to know what C:/path/to/your/file is, because this is a path to a file on your local machine. The proper way to send this file data is to open the file in Java, and then read it and write it to the HttpUrlConnection's output stream.
For example, see the code snippet under "Posting Content" on the Android documentation's discussion of HttpUrlConnection: http://developer.android.com/reference/java/net/HttpURLConnection.html

java.io.FileNotFoundException while uploading a file to web server

I am trying to upload a log file from an applet. The applet uploads a file to a web application (environment Struts 2, Jboss) and receives response (string) from the server
I am using following code to connect, upload a (log) file and receive server response from a application hosted on localhost on Jboss, running at port 8080 :
byte[] myData = aData.getBytes();
/* Uploading the data */
URL myURL = new URL(aURL);
URLConnection myConnection = myURL.openConnection();
myConnection.setDoOutput(true);
myConnection.setUseCaches(false);
myConnection.setDefaultUseCaches(false);
myConnection.setRequestProperty("Content-type", "application/octet-stream");
OutputStream myOutputStream = myConnection.getOutputStream();
myOutputStream.write(myData);
myOutputStream.flush();
myOutputStream.close();
/* Getting the response */
InputStream myInputStream = myConnection.getInputStream();
byte myBytes[] = new byte[1024];
StringBuffer myStringBuilder = new StringBuffer();
int myReadCount = myInputStream.read(myBytes);
while (myReadCount > 0) {
myStringBuilder.append(new String(myBytes, 0, myReadCount));
myReadCount = myInputStream.read(myBytes);
}
return myStringBuilder.toString();
On server side, Struts 2 is being used and and action is called to receive this file. The Following code is called at server side :
InputStream inputStream = request.getInputStream();
byte[] appletLog = UploadUtil.readFromInputStream(inputStream);
//appletLog saved in db here;
return UPLOAD_RESPONSE_SUCCESS;
Please note that the server side code executes fine, without any exception etc and file is saved in database successfully.
But right after that a java.io.FileNotFoundException: is thrown at line [EDIT] InputStream myInputStream = myConnection.getInputStream();. I could not find the reason. I would really appreciate if someone can point out mistakes and provide Hints.

Java and FTP to edit online text files

In my Java swing program, I read, edit and save various text files in a local folder using Scanner and BufferedWriter. Is there an easy way I can keep my current code, but, using FTP, edit a web file rather than a local file? Thanks everyone.
You can use the URL and URLConnection classes to obtain InputStreams and OutputStreams to files located on an FTP Server.
To read a file
URL url = new URL("ftp://user:pass#my.ftphost.com/myfile.txt");
InputStream in = url.openStream();
to write a file
URL url = new URL("ftp://user:pass#my.ftphost.com/myfile.txt");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStream out = conn.getOutputStream();
I tried to achieve the same and the answers to those questions helped me a lot:
Adding characters to beginning and end of InputStream in Java (the one marked as right shows how to add a custom string to the InputStream)
Uploading a file to a FTP server from android phone? (the one from Kumar Vivek Mitra shows how to upload a file)
I added new text to the end of my online file like this:
FTPClient con = null;
try {
con = new FTPClient();
con.connect(Hostname);
if (con.login(FTPUsername, FTPPassword)) {
con.enterLocalPassiveMode(); // important!
con.setFileType(FTP.BINARY_FILE_TYPE);
InputStream onlineDataIS = urlOfOnlineFile.openStream();
String end = "\nteeeeeeeeeeeeeeeeest";
List<InputStream> streams = Arrays.asList(
onlineDataIS,
new ByteArrayInputStream(end.getBytes()));
InputStream resultIS = new SequenceInputStream(Collections.enumeration(streams));
// Stores a file on the server using the given name and taking input from the given InputStream.
boolean result = con.storeFile(PathOfTargetFile, resultIS);
onlineDataIS.close();
resultIS.close();
if (result) Log.v("upload result", "succeeded");
con.logout();
con.disconnect();
}
return "Writing successful";
} catch (IOException e) {
// some smart error handling
}
Hope that helps.

Categories