public int uploadFile1(String asbIMG) {
HttpURLConnection conn = null;
int maxBufferSize = 20 * 1024 * 1024;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
BufferedReader bufferedReader = null;
FileInputStream fs1 = null;
try {
fs1 = new FileInputStream(asbIMG);
URL url = new URL(upLoadServerUri);
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", asbIMG);
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes(
"Content-Disposition: form-data; name=\"uploaded_file\";filename=\"" + asbIMG + "\"" + lineEnd);
dos.writeBytes(lineEnd);
int byteAvailable1 = fs1.available();
int buffersize1 = Math.min(byteAvailable1, maxBufferSize);
byte[] buffer = new byte[buffersize1];
int bytesRead = fs1.read(buffer, 0, buffersize1);
while (bytesRead > 0) {
dos.write(buffer, 0, buffersize1);
byteAvailable1 = fs1.available();
buffersize1 = Math.min(byteAvailable1, maxBufferSize);
bytesRead = fs1.read(buffer, 0, buffersize1);
}
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
fs1.close();
dos.flush();
int ch;
InputStream is = conn.getInputStream();
StringBuffer b = new StringBuffer();
while((ch=is.read())!=1){
b.append((char)ch);
}
String s = b.toString();
Log.i("Test", s);
;
dos.close();
}
안드로이드에서 php로는 multipart로 이미지 전송이 제대로 가는데
asp.net으로는 post로 데이터 값만 받을 뿐 파일은 넘겨받지 못하내요..
그런데 asp.net로 만든 코드를 웹 html에서 multipart방식으로 이미지를 넘기면 또 제대로 동작하네요...
안드로이드에서 asp.net으로 이미지 넘길때 더 추가해야 할게 있나요?
ASP.net
HttpPostedFile file = Request.Files["uploaded_file"];
// check file was submitted
if (file != null && file.ContentLength > 0)
{
string fname = Path.GetFileName(file.FileName);
file.SaveAs(Server.MapPath(Path.Combine("/formpost/Upload/", fname)));
}