마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

안드로이드 jsp연동해서 파일다운로드하고싶습니다.

0 추천
 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/ 에 파일이 다운받아질꺼라고 생각이드는데 혹시 아닌가요?

어떻게해야할까요?

감사합니다

차우차우1231231 (140 포인트) 님이 2020년 9월 13일 질문

1개의 답변

0 추천
127.0.0.1은 자신입니다.

컴퓨터에서는 컴퓨터 자신이고, 폰에서는 폰 자신이 되는거죠.

폰에서 127.0.0.1 에 접속하면 자기 자신의 어딘가에 있는 파일을 받아오는거라서 안되는게 정상입니다.
쎄미 (162,410 포인트) 님이 2020년 9월 15일 답변
그건 알고있습니다. 올릴때 수정해서 올린것입니다
그런데 이제 sdcard에 파일을 다운받으려고하는데 다운이 받아지지않습니다
ㅜㅜ
답변감사합니다
...