I'm trying to upload a file to a webserver. I have seen some code online but i think the code is to extensive and i dont need all that (maybe i'm outrageously wrong). My approach is the following one:
public void upLoadFile(String url, String file_path) throws MalformedURLException, FileNotFoundException {
try {
File file = new File(file_path);
URL u = new URL(url);
URLConnection conn = u.openConnection();
conn.setDoOutput(true);
DataInputStream stream = new DataInputStream(new FileInputStream(file) );
byte[] buffer = new byte[(int) file.length()];
stream.readFully(buffer);
stream.close();
DataOutputStream fos = new DataOutputStream(conn.getOutputStream());
fos.write(buffer);
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
I call that funcion like this:
upLoadFile("http://192.168.1.144/NewFile.txt", "C:\\FileToUpload.txt")
It is supposed to read bytes from a file and write them down on a file in the webserver. Well, i may be wrong but this should be as simple as this. Right? The problem is that, although i dont get any errors or exceptions, i cant manage to upload the file. Honestly, apache's Logs does not even report an acess or something like that.
Could you please help me?
Thanks in advance