2013-05-26 2 views
1

저는 Java를 사용하여 파일을 다운로드 한 후 다른 디렉토리로 이동하기위한 스크립트 또는 자습서를 둘러 보았습니다. 내 질문과 비슷한 질문을 많이 보았지만 모두 다 다릅니다. 이 작업을 수행하는 확실한 방법이 있습니까? 나는 현재 자바를 배우고 충분한 경험이 없기 때문에 이에 대한 기능을하는 스크립트를 만든다. 아무도 도와 줄 수 있습니까?java를 사용하여 파일을 다운로드하고 디렉토리로 이동하는 방법은 무엇입니까?

+0

"다운로드 및 이동"이란 인터넷에서 지정된 폴더로 파일을 다운로드한다는 의미입니까? –

+0

예 죄송합니다, 지정하지 않았습니다 – user2422742

답변

2

개인적으로 이것이 웹에서 파일을 다운로드하는 가장 좋은 방법이라고 생각합니다. 파일을 다운로드 할 때 파일을 지정하지 않으면 하드 드라이브가 아니라 현재 실행중인 프로그램에 저장됩니다.

  URL url; 
      URLConnection con; 
      DataInputStream dis; 
      FileOutputStream fos; 
      byte[] fileData; 
      try { 
       url = new URL("http://website.com/file.pdf"); //File Location goes here 
       con = url.openConnection(); // open the url connection. 
       dis = new DataInputStream(con.getInputStream()); 
       fileData = new byte[con.getContentLength()]; 
       for (int q = 0; q < fileData.length; q++) { 
        fileData[q] = dis.readByte(); 
       } 
       dis.close(); // close the data input stream 
       fos = new FileOutputStream(new File("/Users/kfang/Documents/Download/file.pdf")); //FILE Save Location goes here 
       fos.write(fileData); // write out the file we want to save. 
       fos.close(); // close the output stream writer 
      } 
      catch(Exception m) { 
       System.out.println(m); 
      } 
+0

고마워, 이걸 시도해 볼게. – user2422742

관련 문제