1

I'm trying to write a file and upload it, however, the file does not seem to be written properly (as later on that I need to upload it, it crashes and says no file). I'm following the guidelines of Google's documentation. Here's my code:

 String fileLocation = "Hello"; String TESTSTRING = new String("Hello Android"); FileOutputStream fOut = openFileOutput(fileLocation, MODE_WORLD_READABLE); fOut.write(TESTSTRING.getBytes()); fOut.close(); 

That's how I'm trying to upload:

 HttpURLConnection connection = null; DataOutputStream outputStream = null; DataInputStream inputStream = null; String pathToOurFile = fileLocation; String Tag = "UPLOADER"; HttpURLConnection conn = null; String urlServer = "http://..."; //my server String lineEnd = "\r\n"; String twoHyphens = "--"; String boundary = "*****"; try { // ------------------ CLIENT REQUEST Log.e(Tag, "Inside second Method"); FileInputStream fileInputStream = new FileInputStream(new File(fileLocation)); // open a URL connection to the Servlet URL url = new URL(urlServer); // Open a HTTP connection to the URL conn = (HttpURLConnection) url.openConnection(); // Allow Inputs conn.setDoInput(true); // Allow Outputs conn.setDoOutput(true); // Don't use a cached copy. conn.setUseCaches(false); // Use a post method. conn.setRequestMethod("POST"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); DataOutputStream dos = new DataOutputStream(conn.getOutputStream()); dos.writeBytes(twoHyphens + boundary + lineEnd); dos .writeBytes("Content-Disposition: post-data; name=uploadedfile;filename=" + fileLocation + "" + lineEnd); dos.writeBytes(lineEnd); Log.e(Tag, "Headers are written"); // create a buffer of maximum size int bytesAvailable = fileInputStream.available(); int maxBufferSize = 1000; // int bufferSize = Math.min(bytesAvailable, maxBufferSize); byte[] buffer = new byte[bytesAvailable]; // read file and write it into form... int bytesRead = fileInputStream.read(buffer, 0, bytesAvailable); while (bytesRead > 0) { dos.write(buffer, 0, bytesAvailable); bytesAvailable = fileInputStream.available(); bytesAvailable = Math.min(bytesAvailable, maxBufferSize); bytesRead = fileInputStream.read(buffer, 0, bytesAvailable); } // send multipart form data necesssary after file data... dos.writeBytes(lineEnd); dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); // close streams Log.e(Tag, "File is written"); fileInputStream.close(); dos.flush(); dos.close(); } catch (MalformedURLException ex) { Log.e(Tag, "error: " + ex.getMessage(), ex); } catch (IOException ioe) { Log.e(Tag, "error: " + ioe.getMessage(), ioe); } try { BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; while ((line = rd.readLine()) != null) { Log.e("Dialoge Box", "Message: " + line); } rd.close(); } catch (IOException ioex) { Log.e("MediaPlayer", "error: " + ioex.getMessage(), ioex); } } 

Here's the PHP code on the server:

$target_path = "./"; $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; } else{ echo "There was an error uploading the file, please try again!"; } 
8
  • Which part crashes, writing the file or reading the file? Commented Nov 29, 2011 at 16:46
  • @gyoda reading the file later for uploading. can't find the file. Commented Nov 29, 2011 at 16:49
  • And btw I'm using the same fileLocation string as the file name later for uploading it. Commented Nov 29, 2011 at 16:54
  • Can you post the code you are using to try and read the file later? And are you doing the reading in the same activity or a different activity? Commented Nov 29, 2011 at 16:55
  • I've never seen an Android program "crash" gracefully saying "No file". You say their is no logcat error or stack trace, can you give us anything else to go by besides "no file"? Commented Nov 29, 2011 at 16:57

3 Answers 3

2

Instead of using

FileInputStream fileInputStream = new FileInputStream(new File(fileLocation)); 

use

FileInputStream fileInputStream = openFileInput(fileLocation); 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! It no longer crashes, however, the file is still not being uploaded. Can you have a look at the code? I've updated the first post with the PHP code of the server.
I'm not really a PHP guy, so I'm going to have to pass on that one. I'd recommend opening a new question with the PHP code, since this question was really more about Android file writing/reading. And accept my answer if it helped you out ;)
0

try something like this:

fileLocation = context.getFilesDir() + "Hello"; 

I'm not sure that you can/should write files to the root directory like that.

1 Comment

Thanks Joe, I think it writes that to my apps local storage (not root). Anyway the issue was with reading it back, not writing. It's solved now!
0

Please, first Write a String like this, than you send the file to server. It will help some one.

 String resp = "Hello Andrid!!!"; File file= new File("/sdcard/hello.xml"); FileOutputStream fos = new FileOutputStream(file); try { fos.write(resp.getBytes()); fos.flush(); fos.close(); Log.d("File Write is success","fine"); } catch (Exception e) { Log.d("Error in File write: ", ""+e.getMessage()); } finally { if (fos != null) { fos = null; } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.