我有一个应用程序可以拍照并向我提供给定图片的uri。 接下来,我想做的是将此图片发送到我的网络服务。
但是,一旦我尝试了,我得到了一个我之前遇到的错误,这与asynctask有关。所以试图解决它,我有一个HttpManager类,它包含有关如何连接到webservice,url本身以及它处理图像uri的位置的信息。
public static String uploadImageToWebservice(String uri, String imageUri) {
HttpURLConnection connection = null;
int responseCode = 0;
String image = "";
try {
URL url = new URL(uri + imageUri);
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("GET");
connection.setRequestProperty("Content-Type", "text/plain");
/*
* String userpassword = adminUser + ":" + adminPassword; String
* encodedAuthorization = DatatypeConverter
* .printBase64Binary(userpassword.getBytes("UTF-8"));
* connection.setRequestProperty("Authorization", "Basic " +
* encodedAuthorization);
*/
InputStream is = connection.getInputStream();
image = is.toString();
responseCode = connection.getResponseCode();
connection.disconnect();
} catch (IOException e) {
Log.e("URL", "failure response from server >" + e.getMessage()
+ "<");
} finally {
if (connection != null) {
connection.disconnect();
}
}
return image;
}
这就是我在活动中处理这种方法的方法。
private void submitImage(String uri) {
HttpAsyncTask at = new HttpAsyncTask();
at.execute(uri);
}
private class HttpAsyncTask extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
String image = HttpManager.uploadImageToWebservice(params[0],
params[1]);
return image;
}
}
然后我使用Web服务的Uri在oncreate中调用submitImage方法。
但我有点被困在图像本身的uri放在哪里也可以发送。我只是觉得我错过了什么,我无法弄清楚它在哪里。希望它能够理解所有这一切。
提前致谢!
答案 0 :(得分:0)
使用代码上传图片
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;
FileInputStream fileInputStream = new FileInputStream("sourcefile");
URL url = new URL("URL");
// Open a HTTP connection to the URL
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("bill", sourceFileUri);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"bill\";filename=\""
+ sourceFileUri + "\"" + 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();
if (serverResponseCode == 200) {
statud = "uploaded";
// messageText.setText(msg);
// Toast.makeText(ctx, "File Upload Complete.",
// Toast.LENGTH_SHORT).show();
// System.out.println("Uploaded successfyuly http: 200");
// recursiveDelete(mDirectory1);
}
// close the streams //
fileInputStream.close();
dos.flush();
dos.close();
php code
<?php
$uploads_dir = './images/';
$tmp_name = $_FILES['bill']['tmp_name'];
$pic_name = $_FILES['bill']['name'];
if (move_uploaded_file($tmp_name, $uploads_dir.$pic_name )) {
echo "uploaded";
}
?>