From 61e8e600dd1c9d6066711dfb9874e0117b87baf6 Mon Sep 17 00:00:00 2001 From: Jonathan Bauer Date: Wed, 10 Sep 2014 14:54:40 +0200 Subject: mavenization v2 --- Dozentenmodul/src/main/java/ftp/UploadTask.java | 178 +++++++++++++----------- 1 file changed, 94 insertions(+), 84 deletions(-) (limited to 'Dozentenmodul/src/main/java/ftp/UploadTask.java') 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 { - 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 -- cgit v1.2.3-55-g7522