1
나는 안드로이드에 초보자이며 이미지를 안드로이드에 업로드하는 데 문제가 있습니다. 내 코드를 여기에업로드 이미지 HTTP POST Android
과 : 난 이미 예를 찾기 위해 시도했지만 운이 없어 것 :
내가 말하는 오류를 데를 내가 "images"
라는 이름의 배열을 포함하는이 업로드하려는 이미지의 경로 예. /storage/sdcard0/DCIM/Camera/IMG_20131214_171438.jpg
//ONCLICK LISTENER
public OnClickListener upload_image = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(images.size() != 0) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
for(int i=0; i<images.size(); i++){
doFileUpload(images.get(i));
}
}
}
};
//은
private void doFileUpload(String upload_file){
Log.d("Currently queued", upload_file);
HttpURLConnection conn = null;
DataOutputStream dos = null;
DataInputStream inStream = null;
String exsistingFileName = upload_file;
// Is this the place are you doing something wrong.
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "acebdf13572468";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;
String urlString = "http://test.xponlm.com/MobileServices/api/ShipmentImages";
try
{
Log.e("MediaPlayer","Inside second Method");
FileInputStream fileInputStream = new FileInputStream(new File(exsistingFileName));
URL url = new URL(urlString);
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
// Allow Outputs
conn.setDoOutput(true);
// Don't use a cached copy.
conn.setUseCaches(false);
// Use a post method.
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + exsistingFileName +"\"" + lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"ID\""+ lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes(strshipmentID + lineEnd);
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"ImageType\""+ lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes("JPG" + lineEnd);
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"FileType\""+ lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes(strtype + lineEnd);
Log.e("MediaPlayer","Headers are written");
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);
}
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
// tv.append(inputLine);
Log.d("inputLine", inputLine);
// close streams
Log.e("MediaPlayer","File is written");
fileInputStream.close();
dos.flush();
dos.close();
}
catch (MalformedURLException ex)
{
Log.e("MediaPlayer", "error1: " + ex.getMessage(), ex);
}
catch (IOException ioe)
{
Log.e("MediaPlayer", "error2: " + ioe.getMessage(), ioe);
}
//------------------ read the SERVER RESPONSE
try {
inStream = new DataInputStream (conn.getInputStream());
String str;
while ((str = inStream.readLine()) != null) {
Log.e("MediaPlayer","Server Response"+str);
}
/*while((str = inStream.readLine()) !=null){
}*/
inStream.close();
}
catch (IOException ioex){
Log.e("MediaPlayer", "error3: " + ioex.getMessage(), ioex);
}
}
내가 뭔가를해야만 놓친나요 업로드 하시겠습니까? 내 코드의 문제점을 알 수 있습니까?
나는 이것을 시도 할 것이다 : – Lian
당신은 당신의 해결책을 찾았습니까 ?? –
어디에서 MultipartEntity 클래스를 얻을 수 있습니까? 내용은 무엇입니까? – Lian