스마트폰으로 웹서버에 이미지파일 전송테스트 중인데
진저브레드환경에서 잘 동작하던 DataOutputStream이 아이스크림 샌드위치에서 부턴 오류가 납니다.
로그를 찍어보니 아래 파란줄 부분까지만 출력이 되고 Exception 되는데 버전문제 때문인가요?
소스를 올려 보겠습니다.
public void doFileUpload(String mfile, String mid, String mpass){
try {
mFileInputStream = new FileInputStream(mfile.toString());
String uploadUrl = "http://xxx.xxx.xxx.xxx:8000/RequestImage/image_upload.jsp?id="+ mid +"&pass="+ mpass;
connectUrl = new URL("uploadUrl);
// 커넥션 객체를 열어준다.
HttpURLConnection conn = (HttpURLConnection) connectUrl.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
Log.d("/////////", "0");
// write data
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"addfile1\";filename=\"" + mfile + "\"" + lineEnd);
dos.writeBytes(lineEnd);
int bytesAvailable = mFileInputStream.available();
int maxBufferSize = 1024;
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte[] buffer = new byte[bufferSize];
int bytesRead = mFileInputStream.read(buffer, 0, bufferSize);
// read image
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = mFileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = mFileInputStream.read(buffer, 0, bufferSize);
}
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// close streams
Log.d("======>⑤", "File is written");
mFileInputStream.close();
dos.flush(); // finish upload...
// get response
int ch;
InputStream is = conn.getInputStream();
StringBuffer b = new StringBuffer();
while ((ch = is.read()) != -1) {
b.append((char) ch);
}
urlname = b.toString();
SharedPreferences prefs = getSharedPreferences("urlname", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("urlname", urlname);
editor.commit();
dos.close();
Intent intent2 = new Intent(Information.this, C25_SHCamera.class);
startActivity(intent2);
finish();
} catch (Exception e) {
Log.e("======>!!!", "exception :" + e.getMessage());
}
}
스레드에서 처리하시면 됩니다.