갤러리에서 이미지를 선택해서
node.js서버로 업로드 하고싶습니다.
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
DataOutputStream ds = new DataOutputStream(conn.getOutputStream());
File file = new File(absolutePath);
FileInputStream fs = new FileInputStream(file);
Log.d("메시지", "파일 : "+file);
try {
// opening boundary line
ds.writeBytes(twoHyphens + boundary + this.crlf);
ds.writeBytes("Content-Disposition: form-data; name=\""
+ "bitmap.jpg"
+ "\""
+ this.crlf);
ds.writeBytes("Content-Type: " + "application/octet-stream" + this.crlf);
ds.writeBytes(this.crlf);
// create a buffer of maximum size
int bytesAvailable = fs.available();
int maxBufferSize = 1024;
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte[] buffer = new byte[bufferSize];
// read file and write it into form...
int bytesRead = fs.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
ds.write(buffer, 0, bufferSize);
bytesAvailable = fs.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fs.read(buffer, 0, bufferSize);
}
// closing CRLF
ds.writeBytes(this.crlf);
}
catch(Exception e) {
Log.e(TAG, "AndroidUploader.writeFormField: got: " + e.getMessage());
}
ds.writeBytes(twoHyphens + boundary + twoHyphens + this.crlf);
ds.flush();
ds.close();
갤러리에서 사진을 선택한 후 얻은, 사진의 절대경로를 File의 생성자로 주었습니다. 이게 맞나요??
이 코드를 사용해 서버에 이미지를 보내면 콘솔창이 깨집니다..
content-type을 image/jpg로 바꿔도 안되네요..
어디가 잘못된건지 모르겠어요
