public int uploadFile(String sourceFileUri) {
File extBaseDir = Environment.getExternalStorageDirectory();
File file = new File(extBaseDir.getAbsoluteFile() + "/Nrega/data");
// String upLoadServerUri =
// "http://117.239.190.115/MobilePHP/upload_media_test1.php";
String upLoadServerUri = "Server path";
final String fileName = sourceFileUri;
String fileName1 = file.getAbsolutePath() + "/" + sourceFileUri;
HttpURLConnection conn = null;
DataOutputStream dos = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
File sourceFile = new File(fileName1);
if (!sourceFile.isFile()) {
Log.e("uploadFile", "Source File Does not exist");
return 0;
}
try { // open a URL connection to the Servlet
FileInputStream fileInputStream = new FileInputStream(sourceFile);
URL url = new URL(upLoadServerUri);
conn = (HttpURLConnection) url.openConnection(); // Open a HTTP
// connection
// to
// the URL
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());
// dos1 = new DataInputStream(conn.getInputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""
+ fileName + "\"" + lineEnd);
dos.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available(); // create a buffer
// of
// maximum size
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) {
Toast.makeText(this,
fileName + "\nFile Upload to Server Complete.",
Toast.LENGTH_SHORT).show();
/*
* runOnUiThread(new Runnable() { public void run() {
*
* Toast.makeText(getActivity(), fileName +
* "\nFile Upload to Server Complete.",
* Toast.LENGTH_SHORT).show(); } });
*/
} else {
/*
* new Thread(new Runnable() { public void run() {
* runOnUiThread(new Runnable() { public void run() {
* Toast.makeText(getActivity(), "Error in Sending File",
* Toast.LENGTH_SHORT).show();
*
* } }); File extBaseDir = Environment
* .getExternalStorageDirectory(); File file = new
* File(extBaseDir.getAbsoluteFile() + "/GeoStamping/sys");
* final String filePath = file.getAbsolutePath() + "/" +
* "unsent.dat"; File ff = new File(filePath); try { String[] d
* = preread(); BufferedWriter out = new BufferedWriter( new
* FileWriter(filePath)); for (int j = 0; j < d.length; j++) {
* out.write(d[j].toString() + "\n"); } out.write(txtfilename2 +
* ".txt\n"); out.write(txtfilename2 + ".jpg\n"); out.flush();
* out.close(); } catch (Exception e) { // TODO Auto-generated
* catch block e.printStackTrace(); } } }).start();
*/
}
// close the streams //
fileInputStream.close();
dos.flush();
dos.close();
} catch (final MalformedURLException e) {
showError("Error : MalformedURLException ");
// showError("Error : MalformedURLException " + e);
e.printStackTrace();
} catch (final IOException e) {
showError("Error : IOException ");
// showError("Error : IOException " + e);
e.printStackTrace();
} catch (final Exception e) {
// int er = e.printStackTrace();
// showError(e.printStackTrace());
showError(e.toString());
}
// dialog.dismiss();
return serverResponseCode;
}
它为我工作上传任何文件,但我没有得到使用此方法上传的图像文件的路径,请帮助我在android中非常婴儿