All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.viaoa.jfc.OAFtpProgressBar Maven / Gradle / Ivy

/*  Copyright 1999-2015 Vince Via [email protected]
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/
package com.viaoa.jfc;

import javax.swing.*;
import java.net.*;
import java.io.*;

import com.viaoa.util.*;

/**
    JProgressBar component used to receive a file using FTP, and visually display the progress.
    

For more information about this package, see documentation. */ public class OAFtpProgressBar extends JProgressBar { protected String host; protected String user; protected String password; protected boolean binary; protected int estSize; public OAFtpProgressBar() { } /** Computer to receive file from. */ public void setHost(String host) { this.host = host; } /** FTP Server to receive file from. */ public String getHost() { return host; } /** Name to use for logging into FTP server. */ public void setUser(String user) { this.user = user; } /** Name to use for logging into FTP server. */ public String getUser() { return user; } /** Password to use for logging into FTP server. */ public void setPassword(String password) { this.password = password; } /** Password to use for logging into FTP server. */ public String getPassword() { return password; } /** If true, then file will be received in binary mode, else text mode. */ public void setBinary(boolean b) { binary = b; } /** If true, then file will be received in binary mode, else text mode. */ public boolean getBinary() { return binary; } /** Used to set the status max value for the progress bar. */ public void setEstimatedSize(int x) { estSize = x; } /** Used to set the status max value for the progress bar. */ public int getEstimatedSize() { return estSize; } /** Receive a file from FTP server. @param fileName file name on server to retrieve. @param saveAsFileName name of file to store. */ public void getFile(String fileName, String saveAsFileName) throws Exception { // ftp: FTP.VetPlan.net is 64.243.72.44 user: vetplanvince pw: vetplan83is83 String sUrl = ""; if (user != null) sUrl += user + ":" + password + "@"; sUrl += host; sUrl += "/"+fileName; if (binary) sUrl += ";type=i"; URL url = new URL("ftp://" + sUrl); // ftp://vetplanvince:[email protected]/oa.jar;type=i /**** ";type=", "where typecode is one of the characters 'a', 'i', or 'd'." (In this case, you want ";type=a"; 'i' is for image (binary) transfer; 'd' is a directory. ****/ URLConnection con = url.openConnection(); this.setMinimum(0); this.setMaximum(estSize); this.setValue(0); InputStream is = con.getInputStream(); String s = saveAsFileName; s = s.replace('\\', '/'); int x = s.lastIndexOf('/'); if (x >= 0) { s = s.substring(0,x); File file = new File(OAString.convertFileName(s)); file.mkdirs(); } saveAsFileName = OAString.convertFileName(saveAsFileName); File file = new File(saveAsFileName); OutputStream os = new FileOutputStream(file); int tot = 0; int bufferSize = 1024 * 2; byte[] bs = new byte[bufferSize]; for (int i=0; ;i++) { x = is.read(bs, 0, bufferSize); if (x < 0) break; tot += x; if (tot > estSize) { this.setMaximum(tot+2048); } this.setValue(tot); os.write(bs, 0, x); } is.close(); os.close(); setValue(getMaximum()); System.out.println("--> "+fileName+" "+tot); } } /********* Peter van der Linden's Linlyn applet http://www.afu.com/t.java // // **************************** Security Warning! ************************ // This is a serious warning, and you need to pay attention to it. // // To work, this program needs you to put your ftp password into the // code as a String. This is only appropriate on an Intranet or when // the applet is on a page to which access is otherwise controlled. // // A malicious person could access the applet class file and easily // retrieve YOUR password. Then they would have access to your ftp // account. They could add, delete, or change files. They could even // change the password. That would be very bad for you. // // Giving away your password is frequently not allowed by the terms of // your account provider. Do NOT use this program unless you are // authorized to pass on your ftp password, and you are able to firewall // the ftp account or otherwise protect against intrusion. One thing // you could do is have a very small ftp area, and only allow existing // files to be appended (for example). // // Creating an applet with this program is like publishing your password // to anyone who can access the applet class file. // // If you still want to proceed, it is at your own risk. // **************************** Security Warning! ************************ // Instructions: // 1. This file contains the applet I/O class Linlyn, and // a sample applet, t, that uses it. // // 2. modify the literal strings in t.java // to use your server, username, passwd etc. // // 3. compile by typing // javac t.java // // 4. try running by: // appletviewer t.java // it will upload and retrieve a file for an applet // (The html to run it is in this file, so t.java not t.html is OK). // // 5. modify t.java to be your own applet // // While getting started, you might want to uncomment the // "debug" lines near the head of Linlyn.java; this will // produce output to help in your initial configuration. // // if this works for you, send a note to [email protected], May 1998. / This runs the Linlyn applet


/ // a test applet to show the use of the Linlyn class // you don't have to embed the passwd in the code - you could // prompt for it. // import java.awt.*; // needed by the applet import java.applet.*; import java.io.*; // needed by Linlyn import java.net.*; import java.util.*; public class t extends Applet { public void init() { try { // upload to file // You must replace the strings with actual values to use. // e.g. Linlyn("microsoft.com","billg", "allmy$$$"); Linlyn u = new Linlyn("YOUR_SERVER", "USERNAME", "PASSWD"); u.upload("pub", "score.txt", "'Just Java 2' is a great Java book! \n" + "available quick and cheap from \n" + " http://www.amazon.com/exec/obidos/ASIN/0130105341/afuinc "); // add some more onto end of existing file Linlyn u2 = new Linlyn("YOUR_SERVER", "USERNAME", "PASSWD"); u2.append("pub", "score.txt", "append this info at ONCE", true); // download from file // You must replace the strings with actual values to use. Linlyn d = new Linlyn("YOUR_SERVER", "USERNAME", "PASSWD"); String s = d.download("pub", "score.txt"); // display file contents TextArea ta = new TextArea( s, 3, 40); add(ta); } catch(java.io.IOException ioe) {ioe.printStackTrace();} } } ////////////////////////////////////////// // split here for Linlyn.java // // At last! Java code to read/write files on the server from an applet! // This is the famous Linlyn code. // // Use: // compile this file, and have your applet call it as below. // // to upload a file: // Linlyn ftp = new Linlyn( , , ); // ftp.upload( , , ); // // to download a file: // Linlyn ftp = new Linlyn( , , ); // String contents = ftp.download( , ); // // the default is ASCII transfer, an overloaded method does bin. // // All parameters and return values are Strings. E.g. // Linlyn ftp = new Linlyn( "rtfm.mit.edu", "anonymous", "linden@" ); // String contents = ftp.download( // "/pub/usenet-by-group/comp.lang.java.programmer" // "Java_Programmers_FAQ" ); // // [the actual values above are not generally valid, substitute // your own server for your first attempt, see note 1.] // // Notes: // 1. Usual applet security rules apply: you can only get a file // from the server that served the applet. // 2. The applet server must also be an FTP server. This is NOT true // for some ISPs, such as best.com. They have separate FTP and // http machines. This code may work on such a setup if you put // the classfiles into the ftp area, and in the HTML file say: //