我正在通过网站把一个文件上载到我的服务器上,即有一个简单的方法使工作得以完成,但装满......方言从没有显示如何确定这一点......这是我的法典。
public void send_data() throws IOException
{
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
String lineEnd = "
";
String twoHyphens = "--";
String boundary = "*****";
ProgressDialog dialog = ProgressDialog.show(CaptureTestActivity.this, "",
"Loading. Please wait...", true);
dialog.show();
String urlServer = "http://poi.gps.ro:80/postimg?lat=" + String.valueOf(mDraw.lat) + "&lon=" + String.valueOf(mDraw.lon)+"&comment=" + tmp;
Log.w("DHA", urlServer);
URL url = new URL(urlServer);
if (url != null)
{
Log.w("DHA", "Merge aici");
connection = (HttpURLConnection) url.openConnection();
if (connection != null)
{
Log.w("DHA", "Si aici mere!");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Host", "poi.gps.ro");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=*****");
outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name="uploadedfile";filename="" + "PICT0000" +""" + lineEnd);
outputStream.writeBytes(lineEnd);
outputStream.write(btarr.toByteArray());
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
Log.w("DHA", "Incep trimiterea pozei!");
outputStream.flush();
outputStream.close();
int serverResponseCode = connection.getResponseCode();
String conn = connection.getResponseMessage();
// InputStream in = connection.getInputStream();
// StringWriter writer = new StringWriter();
Log.w("DHA", conn);
Log.w("DHA", "Serverul a raspuns cu " + String.valueOf(serverResponseCode));
if (serverResponseCode == 200)
{
AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Super :)");
alertDialog.setMessage("Poza a fost trimisa cu success.");
alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
} });
alertDialog.show();
}
}
}
dialog.dismiss();
}