안녕하세요 안드로이드 열공중인데요.. 무지에서 시작하려니 막히는 부분이 많네요.
그래도 여기에서 많은걸 배우면서 하고있는데요.. 지금 하고있는게 SDCARD 에 있는 이미지를 전송해서 웹서버에 저장하는 작업을 테스트 중인데요..
SDCARD에 있는 yoo.jpg 를 http://192.168.10.1/test.asp 로 멀티폼으로 전송시켜 서버에 저장하려고 하고있습니다.
오픈되어있는 소스로 하니까 에러 로그부분에
this is error java.lang.IllegalArgumentException: file /sdcard/yoo.jpg contains a path separator
이런 로그가 있던데 이건 어떠한 에러인지 알려주시면 너무 너무 감사..
외국사이트까지 다 찾아가면서 해보는데도 해결이 안되서 이렇게 문의드립니다.
이것때문에 몇일째 골머리 썩고 있거든요..
알고계신분은 제발 PLZ~~~~ 부탁드립니다.
사용하고있는 예제 소스...
=====================================================================================
String exsistingFileName = "/sdcard/yoo.jpg";
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
String urlString="http://192.168.10.1/test.asp";
HttpURLConnection conn;
DataOutputStream dos;
try{
FileInputStream fileInputStream = openFileInput(exsistingFileName);
InputStreamReader isr = new InputStreamReader(fileInputStream );
char[] inputBuffer = new char[fileInputStream .available()];
isr.read(inputBuffer);
String readString = new String(inputBuffer);
Log.d("Test", "from file"+readString);
//tv.setText("from file"+readString); // ----------> It is reading text from file.
URL url = new URL(urlString);
// Open a HTTP connection to the URL
conn = (HttpURLConnection) url.openConnection();
// Allow Inputs
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);
// Log.e(Tag,"Headers are written");
// create a buffer of maximum size
int maxBufferSize=1024;
int bytesAvailable = fileInputStream.available();
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte[] buffer = new byte[bufferSize];
// read file and write it into form...
int 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 necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// close streams
// Log.e(Tag,"File is written");
fileInputStream.close();
dos.flush();
dos.close();
}catch(Exception e){
Log.e("ERROR", "this is error : "+e.toString());
//tv.setText("this is error"+e.toString());
}
}
}
================================================================================
앗 감사합니다. T^T
드디어 이미지가 업로드가 되네요..
너무너무 감사합니다. 근데 파일 bytesRead 가 0으로 넘어와서 생성된 파일이 0바이트네요..
ㅡㅡ;; 이런.. 하나하나 다 물어보면서 하는건 개발자의 자세가 아니니 하는데 까지 해서 찾아 봐야겠네요.
답변 감사합니다.
댈밴드 님 혹시 asp 소스좀 얻을 수 있을까요? 몇일을 아무리 구글링을 해도 asp 소스는 좀처럼 찾지를 못하겠습니다. 염치 없지만 좀 부탁드려도 될까요? ㅡㅡ;;;
요렇게 해보세요
String mFilePath = "/sdcard/bae1.png"; // 업로드할 파일을 미리 준비 할것
private FileInputStream mFileInputStream = null;
private void DoFileUpload(String mFilePath) throws IOException
{
HttpFileUpload("http://127.0.0.1/web/test.asp" , "" , mFilePath );
}
private void HttpFileUpload(String urlString , String params, String fileName)
{
// TODO Auto-generated method stub
try{
mFileInputStream = new FileInputStream(fileName);
connectUrl = new URL("urlString);
Log.d("File Up" , "mFileInputStream is " + mFileInputStream);
// open connection
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);
DataOutputStream dos = new DataOutputStream( conn.getOutputStream()) ;
dos.writeBytes(twoHyphens + boundary + lineEnd);
// uploadedfile 파일이 ashx 핸들러에서 파일을 찾을 때 사용함으로 이름이 반드시 동일해야함..
// 이름을 바꾸면 ashx 파일에서도 바꿀것.
dos.writeBytes("Content-Disposition:form-data;name=\"uploadedfile\";filename=\"" + fileName + "\"" + 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);
Log.d("File Up", "image byte is " + bytesRead );
// Read 파일
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.e("File Up" , "File is written");
mFileInputStream.close();
dos.flush(); // 버퍼에 있는 값을 모두 밀어냄
//웹서버에서 결과를 받아 EditText 컨트롤에 보여줌
int ch;
InputStream is =conn.getInputStream();
StringBuffer SBuffer = new StringBuffer();
while((ch = is.read()) != -1 )
{
SBuffer.append((char)ch);
}
String file = SBuffer.toString();
Log.e("File Up" , "result = " + file);
mEditEntry.setText(file);
dos.close();
}
catch(Exception e)
{
Log.d("File Up" , "exception " + e.getMessage());
}
}