public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
File_Name = "123.png";
File_extend = "png";
fileURL = "http://127.0.0.1:12168/Server/aaa.jsp"; // URL
Save_folder = "/Download";
// 다운로드 경로를 외장메모리 사용자 지정 폴더로 함.
String ext = Environment.getExternalStorageState();
if(ext.equals(Environment.MEDIA_MOUNTED)) {
Save_Path = Environment.getExternalStorageDirectory().getAbsolutePath() + Save_folder;
}
//파일 다운로드 버튼 클릭
btn_pdfDownload = (Button)findViewById(R.id.btn_pdfDownload);
btn_pdfDownload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
File dir = new File(Save_Path);
// 폴더가 존재하지 않을 경우 폴더를 만듬
if (!dir.exists()) {
dir.mkdir();
}
// 다운로드 폴더에 동일한 파일명이 존재하는지 확인해서 없으면 다운받고 있으면 해당 파일 실행시킴.
if (!new File(Save_Path + "/" + File_Name).exists()) {
progress = ProgressDialog.show(MainActivity.this, "","파일 다운로드중..");
dThread = new DownloadThread(fileURL, Save_Path + "/" + File_Name);
dThread.start();
} else {
//showDownloadFile();
}
}
});
}
// 다운로드 쓰레드로 돌림..
class DownloadThread extends Thread {
String ServerUrl;
String LocalPath;
DownloadThread(String serverPath, String localPath) {
ServerUrl = serverPath;
LocalPath = localPath;
}
@Override
public void run() {
URL imgurl;
int Read;
try {
imgurl = new URL(ServerUrl);
HttpURLConnection conn = (HttpURLConnection) imgurl.openConnection();
conn.connect();
int len = conn.getContentLength();
byte[] tmpByte = new byte[len];
InputStream is = conn.getInputStream();
File file = new File(LocalPath);
FileOutputStream fos = new FileOutputStream(file);
for (;;) {
Read = is.read(tmpByte);
if (Read <= 0) {
break;
}
fos.write(tmpByte, 0, Read);
}
is.close();
fos.close();
conn.disconnect();
} catch (MalformedURLException e) {
Log.e("ERROR1", e.getMessage());
} catch (IOException e) {
Log.e("ERROR2", e.getMessage());
e.printStackTrace();
}
// mAfterDown.sendEmptyMessage(0);
}
}
그냥 인터넷 url로 jsp 주소로 접속하면 다운이 잘받아지는데 앱으로는 다운이 안받아집니다.
제가 생각했을때는 이렇게하면 sdcard/download/ 에 파일이 다운받아질꺼라고 생각이드는데 혹시 아닌가요?
어떻게해야할까요?
감사합니다