У меня есть изображение из намерений галереи. То есть я получил uri для изображения в android. Как написать изображение или файл в httpsurlconnection для загрузки на сервер. Help.Как написать изображение или файл для httpsurlconnection?
0
A
ответ
0
InputStream iStream = context.getContentResolver().openInputStream(uri);
//open httpurlconnection and configure.
OutputStream oos = (OutputStream) httpConnection.getOutputStream();
try
{
int c;
byte bufr[] = new byte[4096];
while((c = iStream.read(bufr, 0, 4096)) != -1)
{
oos.write(bufr, 0, c);
}
oos.flush();
}
finally
{
if(oos != null)
{
oos.close();
}
iStream.close();
}
-1
В коде Java:
File sourceFile = new File("YOUR IMAGE URL")
FileInputStream fileInputStream = new FileInputStream(
sourceFile);
URL url = new URL("your upload 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("uploaded_file", filePath);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data;name=\"uploaded_file\";filename=\""
+ filePath + "\"" + lineEnd);
dos.writeBytes(lineEnd);
// create a buffer of maximum size
bytesAvailable = fileInputStream.available();
bufferSize = Math
.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
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 necessary after file
// data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens
+ lineEnd);
System.out.println("connection");
// Responses from the server (code and message)
serverResponseCode = conn.getResponseCode();
if (serverResponseCode == 200) {
String msg = "File Upload Completed.\n\n See uploaded file here : \n\n"
+ " http://www.androidexample.com/media/uploads/";
System.out.println("message ----" + msg);
}
fileInputStream.close();
dos.flush();
dos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
И если вы используете PHP API
<?php
$file_path = "PostImage/";
$file_path = $file_path . basename($_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
echo "success";
} else{
echo "fail";
}
>
См эту ссылку: HTTP: //androidexample.com/Upload_File_To_Server_ -_Android_Example/index.php? вид = article_discription & помощь = 83 & aaid = 106 – Piyush