I'm building an Android recorder app, and trying to upload these files to a server using a php script, something I am completely unfamiliar with. I'm using this website as my guide: http://androidexample.com/Upload_File_To_Server_-_Android_Example/index.php?view=article_discription&aid=83&aaid=106
Currently, I get an upload successful message, but then there doesn't appear to be an uploaded file on the server. Perhaps I'm looking in the wrong place? If my php script says $file_path = "uploads/"; and my php script is in var/www/sub/fileupload.php, where on the path should I expect my file to upload to?
Alternatively, I suspect there's an issue with the line where I'm actually uploading the file. The example says
dos.writeBytes("Content-Disposition: form-data; name="uploaded_file";filename=""+ fileName + """ + lineEnd);
This line clearly has some kind of issue with quote matching: maybe a typo? Maybe a lack of escape? Also uploaded_file is undefined. I can adjust it, but I don't understand what the different fields are. To get it to run, I changed it to:
dos.writeBytes("Content-Disposition: form-data; name="+ fileName + ";filename="+ fileName + lineEnd);
This makes it run, but feels off to me. The relevant chunk of code is:
try {
// open a URL connection to the Servlet
FileInputStream fileInputStream = new FileInputStream(sourceFile);
URL url = new URL(upLoadServerUri);
// Open a HTTP connection to the URL
// See http://developer.android.com/reference/java/net/HttpURLConnection.html
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true); // Allow Inputs
conn.setDoOutput(true); // Allow Outputs
conn.setUseCaches(false); // Don't use a Cached Copy
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE", "multipart/form-data");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("uploaded_file", fileName);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name="uploaded_file";filename=""
+ fileName + """ + lineEnd);
dos.writeBytes(lineEnd);
// create a buffer of maximum size
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// read file and write it into form...
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
// send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// Responses from the server (code and message)
serverResponseCode = conn.getResponseCode();
String serverResponseMessage = conn.getResponseMessage();
Log.i("uploadFile", "HTTP Response is : " + serverResponseMessage + ": " + serverResponseCode);
if(serverResponseCode == 200){
runOnUiThread(new Runnable() {
public void run() {
String msg = "File Upload Completed.";
upload_status.setText(msg);
Toast.makeText(RecordActivity.this, "File Upload Complete.",
Toast.LENGTH_SHORT).show();
}
});
}
//close the streams //
fileInputStream.close();
dos.flush();
dos.close();
Note: this question is extremely similar to Android fileupload not working, but I found the answers unhelpful.
0 comments:
Post a Comment