コード例 #1
0
    private void downloadFile(String theFilename) {
      OutputStream outStream = null;
      InputStream is = null;
      try {
        myMessage.stateChanged("DOWNLOADLOCALBASE");
        String theFile =
            myProperties
                .getProperty("BASEURL")
                .substring(0, myProperties.getProperty("BASEURL").lastIndexOf("/"));

        theFile = theFile + "/" + (new URI((theFilename).replaceAll(" ", "%20")));
        URLConnection uCon = null;
        int size = 1024;
        URL Url;
        byte[] buf;
        int ByteRead, ByteWritten = 0;
        Url = new URL(theFile);
        String theLocalFilename = (String) myFileList.get(theFilename).get(2);
        long theFileSize = Long.parseLong((String) myFileList.get(theFilename).get(5));
        outStream =
            new BufferedOutputStream(
                new FileOutputStream(
                    myProperties.getProperty("LOCALBASEDIR")
                        + "\\"
                        + myFileList.get(theFilename).get(2)));

        uCon = Url.openConnection();
        is = uCon.getInputStream();
        buf = new byte[size];
        while ((ByteRead = is.read(buf)) != -1) {
          outStream.write(buf, 0, ByteRead);
          ByteWritten += ByteRead;
          myMessage.setProgress((int) getPercentage(ByteWritten, theFileSize));
          // System.out.println(ByteWritten);
        }
        myMessage.messageChanged("Downloaded Successfully.");
        myMessage.messageChanged(
            "File name:\""
                + theLocalFilename
                + "\"\nNo ofbytes :"
                + ByteWritten
                + "Filesize ="
                + theFileSize);
      } catch (Exception e) {
        e.printStackTrace();
      } finally {
        try {
          is.close();
          outStream.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }