diff options
| author | Jonathan Bauer | 2014-09-10 14:54:40 +0200 |
|---|---|---|
| committer | Jonathan Bauer | 2014-09-10 14:54:40 +0200 |
| commit | 61e8e600dd1c9d6066711dfb9874e0117b87baf6 (patch) | |
| tree | c5321291edcc9286fbe75f1cbf1a0548fb78da1d /Dozentenmodul/src/main/java/ftp | |
| parent | Merge branch 'maven' of git.openslx.org:openslx-ng/tutor-module into maven (diff) | |
| download | tutor-module-61e8e600dd1c9d6066711dfb9874e0117b87baf6.tar.gz tutor-module-61e8e600dd1c9d6066711dfb9874e0117b87baf6.tar.xz tutor-module-61e8e600dd1c9d6066711dfb9874e0117b87baf6.zip | |
mavenization v2
Diffstat (limited to 'Dozentenmodul/src/main/java/ftp')
| -rw-r--r-- | Dozentenmodul/src/main/java/ftp/DownloadTask.java | 81 | ||||
| -rw-r--r-- | Dozentenmodul/src/main/java/ftp/FTPUtility.java | 86 | ||||
| -rw-r--r-- | Dozentenmodul/src/main/java/ftp/UploadTask.java | 178 |
3 files changed, 179 insertions, 166 deletions
diff --git a/Dozentenmodul/src/main/java/ftp/DownloadTask.java b/Dozentenmodul/src/main/java/ftp/DownloadTask.java index 83ad2c15..5592adba 100644 --- a/Dozentenmodul/src/main/java/ftp/DownloadTask.java +++ b/Dozentenmodul/src/main/java/ftp/DownloadTask.java @@ -1,6 +1,6 @@ package ftp; -import gui.image.FTPDownloader_GUI; +import gui.image.FTPEditDownloader_GUI; import java.io.File; import java.io.FileOutputStream; @@ -10,91 +10,90 @@ import javax.swing.JOptionPane; import javax.swing.SwingWorker; /** - * Execute file download in a background thread and update the progress. + * Execute file download in a background thread and update the progress. + * * @author www.codejava.net - * + * */ public class DownloadTask extends SwingWorker<Void, Void> { private static final int BUFFER_SIZE = 4096; - + private String host; private int port; private String username; private String password; - private String downloadPath; private String saveDir; - - @SuppressWarnings("unused") - private FTPDownloader_GUI gui; - + private int percentCompleted; + public DownloadTask(String host, int port, String username, - String password, String downloadPath, String saveDir, - FTPDownloader_GUI gui) { + String password, String downloadPath, String saveDir) { this.host = host; this.port = port; this.username = username; this.password = password; this.downloadPath = downloadPath; this.saveDir = saveDir; - this.gui = gui; - + } /** * Executed in background thread - */ + */ @Override protected Void doInBackground() throws Exception { FTPUtility util = new FTPUtility(host, port, username, password); try { util.connect(); - + byte[] buffer = new byte[BUFFER_SIZE]; int bytesRead = -1; long totalBytesRead = 0; - int percentCompleted = 0; - long start=System.nanoTime(); - final double NANOS_PER_SECOND = 1000000000.0; - final double BYTES_PER_MIB = 1024*1024; + percentCompleted = 0; + long start = System.nanoTime(); + final double NANOS_PER_SECOND = 1000000000.0; + final double BYTES_PER_MIB = 1024 * 1024; long fileSize = util.getFileSize(downloadPath); - //gui.setFileSize(fileSize); - + // gui.setFileSize(fileSize); + String fileName = new File(downloadPath).getName(); - + File downloadFile = new File(saveDir + File.separator + fileName); FileOutputStream outputStream = new FileOutputStream(downloadFile); - + util.downloadFile(downloadPath); InputStream inputStream = util.getInputStream(); - - while ((bytesRead = inputStream.read(buffer)) != -1 && isCancelled()==false) { + + while ((bytesRead = inputStream.read(buffer)) != -1 + && isCancelled() == false) { outputStream.write(buffer, 0, bytesRead); totalBytesRead += bytesRead; - //System.out.println(totalBytesRead); - double speed = NANOS_PER_SECOND / BYTES_PER_MIB * totalBytesRead / (System.nanoTime() - start + 1); + // System.out.println(totalBytesRead); + double speed = NANOS_PER_SECOND / BYTES_PER_MIB + * totalBytesRead / (System.nanoTime() - start + 1); percentCompleted = (int) (totalBytesRead * 100 / fileSize); setProgress(percentCompleted); firePropertyChange("speed", 0, speed); - firePropertyChange("filesize", 0,fileSize); - firePropertyChange("bytesread", 0,totalBytesRead); - + firePropertyChange("filesize", 0, fileSize); + firePropertyChange("bytesread", 0, totalBytesRead); + } outputStream.close(); - + util.finish(); } catch (FTPException ex) { - JOptionPane.showMessageDialog(null, "Error downloading file: " + ex.getMessage(), - "Error", JOptionPane.ERROR_MESSAGE); + JOptionPane.showMessageDialog(null, + "Error downloading file: " + ex.getMessage(), "Error", + JOptionPane.ERROR_MESSAGE); ex.printStackTrace(); setProgress(0); - cancel(true); + cancel(true); } finally { util.disconnect(); } - + return null; } @@ -103,10 +102,16 @@ public class DownloadTask extends SwingWorker<Void, Void> { */ @Override protected void done() { - if (!isCancelled()) { + if (!isCancelled() && percentCompleted==100) { + System.out.println("Datei erfolgreich heruntergeladen"); + JOptionPane.showMessageDialog(null, + "Datei erfolgreich heruntergeladen.", "Message", + JOptionPane.INFORMATION_MESSAGE); + } else if(!isCancelled() && percentCompleted != 100){ + System.out.println("Datein wurde unvollständig heruntergeladen"); JOptionPane.showMessageDialog(null, - "File has been downloaded successfully!", "Message", + "Datei wurde unvollständig heruntergeladen. Bitte wiederholen.", "Message", JOptionPane.INFORMATION_MESSAGE); } - } + } }
\ No newline at end of file diff --git a/Dozentenmodul/src/main/java/ftp/FTPUtility.java b/Dozentenmodul/src/main/java/ftp/FTPUtility.java index f2c51dfc..beae3f34 100644 --- a/Dozentenmodul/src/main/java/ftp/FTPUtility.java +++ b/Dozentenmodul/src/main/java/ftp/FTPUtility.java @@ -16,7 +16,6 @@ import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPReply; import org.apache.commons.net.ftp.FTPSClient; - /** * A utility class that provides functionality for downloading files from a FTP * server. @@ -49,7 +48,7 @@ public class FTPUtility { * Connect and login to the server. * * @throws FTPException - * @throws NoSuchAlgorithmException + * @throws NoSuchAlgorithmException */ public void connect() throws FTPException, NoSuchAlgorithmException { try { @@ -58,8 +57,7 @@ public class FTPUtility { if (!FTPReply.isPositiveCompletion(replyCode)) { throw new FTPException("FTP serve refused connection."); } - - + boolean logged = ftpClient.login(username, password); if (!logged) { // failed to login @@ -122,43 +120,45 @@ public class FTPUtility { throw new FTPException("Error downloading file: " + ex.getMessage()); } } - - - /** - * Start uploading a file to the server - * @param uploadFile the file to be uploaded - * @param destDir destination directory on the server - * where the file is stored - * @throws FTPException if client-server communication error occurred - */ - public void uploadFile(File uploadFile, String destDir) throws FTPException { - try { - boolean success = ftpClient.changeWorkingDirectory(destDir); - if (!success) { - throw new FTPException("Could not change working directory to " - + destDir + ". The directory may not exist."); - } - - success = ftpClient.setFileType(FTP.BINARY_FILE_TYPE); - if (!success) { - throw new FTPException("Could not set binary file type."); - } - - outputStream = ftpClient.storeFileStream(Image.image.getNewName()); - - //ftpClient.rename(uploadFile.getName(), ); - } catch (IOException ex) { - throw new FTPException("Error uploading file: " + ex.getMessage()); - } - } - - /** - * Write an array of bytes to the output stream. - */ - public void writeFileBytes(byte[] bytes, int offset, int length) - throws IOException { - outputStream.write(bytes, offset, length); - } + + /** + * Start uploading a file to the server + * + * @param uploadFile + * the file to be uploaded + * @param destDir + * destination directory on the server where the file is stored + * @throws FTPException + * if client-server communication error occurred + */ + public void uploadFile(File uploadFile, String destDir) throws FTPException { + try { + boolean success = ftpClient.changeWorkingDirectory(destDir); + if (!success) { + throw new FTPException("Could not change working directory to " + + destDir + ". The directory may not exist."); + } + + success = ftpClient.setFileType(FTP.BINARY_FILE_TYPE); + if (!success) { + throw new FTPException("Could not set binary file type."); + } + + outputStream = ftpClient.storeFileStream(Image.image.getNewName()); + + // ftpClient.rename(uploadFile.getName(), ); + } catch (IOException ex) { + throw new FTPException("Error uploading file: " + ex.getMessage()); + } + } + + /** + * Write an array of bytes to the output stream. + */ + public void writeFileBytes(byte[] bytes, int offset, int length) + throws IOException { + outputStream.write(bytes, offset, length); + } /** * Complete the download operation. @@ -191,11 +191,9 @@ public class FTPUtility { public InputStream getInputStream() { return inputStream; } - + public OutputStream getOutputStream() { return outputStream; } - - }
\ No newline at end of file diff --git a/Dozentenmodul/src/main/java/ftp/UploadTask.java b/Dozentenmodul/src/main/java/ftp/UploadTask.java index 06d76dc8..e7ebe43b 100644 --- a/Dozentenmodul/src/main/java/ftp/UploadTask.java +++ b/Dozentenmodul/src/main/java/ftp/UploadTask.java @@ -1,98 +1,108 @@ package ftp; - + import java.io.File; import java.io.FileInputStream; - + import javax.swing.JOptionPane; import javax.swing.SwingWorker; import models.Image; - + /** * Executes the file upload in a background thread and updates progress to * listeners that implement the java.beans.PropertyChangeListener interface. + * * @author www.codejava.net - * + * */ public class UploadTask extends SwingWorker<Void, Void> { - private static final int BUFFER_SIZE = 4096; - - private String host; - private int port; - private String username; - private String password; - - private String destDir; - private File uploadFile; - - public UploadTask(String host, int port, String username, String password, - String destDir, File uploadFile) { - this.host = host; - this.port = port; - this.username = username; - this.password = password; - this.destDir = destDir; - this.uploadFile = uploadFile; - - } - - /** - * Executed in background thread - */ - @Override - protected Void doInBackground() throws Exception { - FTPUtility util = new FTPUtility(host, port, username, password); - try { - util.connect(); - util.uploadFile(uploadFile, destDir); - - FileInputStream inputStream = new FileInputStream(uploadFile); - byte[] buffer = new byte[BUFFER_SIZE]; - int bytesRead = -1; - int i=0; - long totalBytesRead = 0; - int percentCompleted = 0; - long fileSize = uploadFile.length(); - Image.image.setFilesize(fileSize); - long start=System.nanoTime(); - final double NANOS_PER_SECOND = 1000000000.0; - final double BYTES_PER_MIB = 1024*1024; - while ((bytesRead = inputStream.read(buffer)) != -1 && isCancelled()==false) { - util.writeFileBytes(buffer, 0, bytesRead); - totalBytesRead += bytesRead; - percentCompleted = (int) (totalBytesRead * 100 / fileSize); - double speed = NANOS_PER_SECOND / BYTES_PER_MIB * totalBytesRead / (System.nanoTime() - start + 1); - setProgress(percentCompleted); - firePropertyChange("speed", 0, speed); - firePropertyChange("filesize", 0,fileSize); - firePropertyChange("bytesread", 0,totalBytesRead); - } - - inputStream.close(); - - util.finish(); - } catch (FTPException ex) { - JOptionPane.showMessageDialog(null, "Error uploading file: " + ex.getMessage(), - "Error", JOptionPane.ERROR_MESSAGE); - ex.printStackTrace(); - setProgress(0); - cancel(true); - } finally { - util.disconnect(); - } - - return null; - } - - /** - * Executed in Swing's event dispatching thread - */ - @Override - protected void done() { - if (!isCancelled()) { - JOptionPane.showMessageDialog(null, - "File has been uploaded successfully!", "Message", - JOptionPane.INFORMATION_MESSAGE); - } - } + private static final int BUFFER_SIZE = 4096; + + private String host; + private int port; + private String username; + private String password; + private String destDir; + private File uploadFile; + private int percentCompleted; + + public UploadTask(String host, int port, String username, String password, + String destDir, File uploadFile) { + this.host = host; + this.port = port; + this.username = username; + this.password = password; + this.destDir = destDir; + this.uploadFile = uploadFile; + + } + + /** + * Executed in background thread + */ + @Override + protected Void doInBackground() throws Exception { + FTPUtility util = new FTPUtility(host, port, username, password); + try { + util.connect(); + util.uploadFile(uploadFile, destDir); + + FileInputStream inputStream = new FileInputStream(uploadFile); + byte[] buffer = new byte[BUFFER_SIZE]; + int bytesRead = -1; + int i = 0; + long totalBytesRead = 0; + percentCompleted = 0; + long fileSize = uploadFile.length(); + Image.image.setFilesize(fileSize); + long start = System.nanoTime(); + final double NANOS_PER_SECOND = 1000000000.0; + final double BYTES_PER_MIB = 1024 * 1024; + while ((bytesRead = inputStream.read(buffer)) != -1 + && isCancelled() == false) { + util.writeFileBytes(buffer, 0, bytesRead); + totalBytesRead += bytesRead; + percentCompleted = (int) (totalBytesRead * 100 / fileSize); + double speed = NANOS_PER_SECOND / BYTES_PER_MIB + * totalBytesRead / (System.nanoTime() - start + 1); + setProgress(percentCompleted); + firePropertyChange("speed", 0, speed); + firePropertyChange("filesize", 0, fileSize); + firePropertyChange("bytesread", 0, totalBytesRead); + } + + inputStream.close(); + + util.finish(); + } catch (FTPException ex) { + JOptionPane.showMessageDialog(null, + "Error uploading file: " + ex.getMessage(), "Error", + JOptionPane.ERROR_MESSAGE); + ex.printStackTrace(); + setProgress(0); + cancel(true); + } finally { + util.disconnect(); + } + + return null; + } + + /** + * Executed in Swing's event dispatching thread + */ + @Override + protected void done() { + if (!isCancelled() && percentCompleted==100) { + System.out.println("Datei erfolgreich hochgeladen"); + JOptionPane.showMessageDialog(null, + "Datei erfolgreich hochgeladen.", "Message", + JOptionPane.INFORMATION_MESSAGE); + } else if(!isCancelled() && percentCompleted != 100){ + System.out.println("Datein wurde unvollständig hochgeladen"); + JOptionPane.showMessageDialog(null, + "Datei wurde unvollständig hochgeladen. Bitte wiederholen.", "Message", + JOptionPane.INFORMATION_MESSAGE); + } + } }
\ No newline at end of file |
