SWT Tool

Kico_One

Grünschnabel
Hallo,

ich bin froh dass ich endlich mal ein Forum gefunden habe indem man geholfen wird,
und natürlich sofort registriert.

Nun ich habe folgendes Problem:
ich soll ein kleines tool entwickeln dass ein uploadvorgang übernimmt.
Ich habe es bis zu einem bestimmten Punkt gelöst nur komme ich nicht weiter.
Ich erklär mal ganz kurz um was es geht:
-Das Tool hat ein 'Drop' Bereich, d.h. man kann da Dateien droppen, der komplette Pfad der Datei wird an einem PHP script übergeben und dieses script läd die datei hoch (läuft auch einwandfrei). Mein Problem ist dass es noch ein Button hat, der nur aktiviert sein soll wenn ein uploadvorgang läuft und zusätzlich soll eine progressbar den Fortschritt anzeigen, sobald der upload erfolgreich war soll der Button disable sein und die Progressbar auf 0% gesetzt werden. Kann mir evtl. jemand helfen?
Das ganze soll mit dem SWT realisiert werden.

Hier noch der Java-Code:
Java:
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.InetAddress;
import java.net.Socket;

import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DragSource;
import org.eclipse.swt.dnd.DragSourceAdapter;
import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.dnd.DropTarget;
import org.eclipse.swt.dnd.DropTargetAdapter;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.FileTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.FontMetrics;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;

import com.swtdesigner.SWTResourceManager;

public class Snippet185 {

public static void main(String[] args) {
	  
	  int style = SWT.SHELL_TRIM|SWT.H_SCROLL|SWT.V_SCROLL;	 	 
	  
	  Display display = new Display();
	  Shell shell = new Shell(display);
	  shell.setText("revis Drop Box");
	  shell.setLayout(new FillLayout());

	  //TableItem item = new TableItem(table, SWT.NONE);
	  //item.setText("Drop a file");
   
	  shell.setSize(360, 185);
	  // final Table table = new Table(shell, SWT.BORDER);
	  // DropTarget dropTarget = new DropTarget(table, DND.DROP_COPY
	  //     | DND.DROP_DEFAULT);
	    
	  final Group group = new Group(shell, style);	  
	  group.setBounds(10, 40, 170, 50);
	  
	  final Label label = new Label(group, SWT.WRAP);
	  label.setFont(SWTResourceManager.getFont("", 9, SWT.BOLD));
	  label.setToolTipText("Dateien in diesem Bereich reinziehen!");
	  //label.setText("Dateien reinziehen...");
	  label.setBounds(10, 20, 322, 80);
    
	  final Button button = new Button(group, SWT.NONE);
	  button.setText("abbrechen");
	  button.setEnabled(false);
	  button.setBounds(10, 115, 70, 25);
    
	  final ProgressBar progressBar = new ProgressBar(group, SWT.NONE);
	  progressBar.setBounds(95,115,237,25);
	  progressBar.setMinimum(0);
	  progressBar.setMaximum(10);
	  
	  progressBar.addPaintListener(new PaintListener() {
	      public void paintControl(PaintEvent e) {
	        String string = (progressBar.getSelection() * 1.0 /
	        		(progressBar.getMaximum()-progressBar.getMinimum()) * 100) + "%";
	        Point point = progressBar.getSize();
	          
	        FontMetrics fontMetrics = e.gc.getFontMetrics();
	        int width = fontMetrics.getAverageCharWidth() * string.length();
	        int height = fontMetrics.getHeight();
	        e.gc.setForeground(group.getDisplay().getSystemColor(SWT.COLOR_WHITE));
	        e.gc.drawString(string, (point.x-width)/2 , (point.y-height)/2, true);
	      }
	   });  	  
	
	  DragSource dragSource = new DragSource(label, DND.DROP_COPY);
	  dragSource.setTransfer(new Transfer[] {FileTransfer.getInstance()});
	  dragSource.addDragListener(new DragSourceAdapter() {
		  public void dragSetData(DragSourceEvent event) {
	 	              	if (FileTransfer.getInstance().isSupportedType(event.dataType)) {
	 	              		File file = new File("temp");
	 	              		event.data = new String[] {file.getAbsolutePath()};
	 	              	}
		  } 
	  });	
	
	  DropTarget dropTarget = new DropTarget(label, DND.DROP_COPY |
			  DND.DROP_DEFAULT);	
	    
	  dropTarget.setTransfer(new Transfer[] {FileTransfer.getInstance() });
	  dropTarget.addDropListener(new DropTargetAdapter() {
      FileTransfer fileTransfer = FileTransfer.getInstance();

      public void dragEnter(DropTargetEvent event) {
        if (event.detail == DND.DROP_DEFAULT)
          event.detail = DND.DROP_COPY;
      }

      public void dragOperationChanged(DropTargetEvent event) {
        if (event.detail == DND.DROP_DEFAULT)
          event.detail = DND.DROP_COPY;
      }          

      public void dragOver(DropTargetEvent event) {
    	
    	  System.out.println(label);
    	  event.detail = DND.DROP_NONE;
        
    	  int index = 0;
    	  while (index < event.dataTypes.length) {
  		  if (fileTransfer.isSupportedType(event.dataTypes[index]))
  			  	break;
  		  		index++;
    	  }
          
          if (index < event.dataTypes.length) {
            event.currentDataType = event.dataTypes[index];
            event.detail = DND.DROP_COPY;
            return;
          }        
      }
      
      
      public void drop(DropTargetEvent event) {    	 
    	  System.out.println(event);        
    	  System.out.println(event.currentDataType);               
        
    	  if (fileTransfer.isSupportedType(event.currentDataType)) {
    		  String[] files = (String[]) event.data;
    		 
    		  System.out.println(files); 
    		  
    		  System.out.println("Dateiengrösse Alle: " +files.length);
    		  
    		  for (int i = 0; i < files.length; i++) {

					  try {Thread.sleep (800);}
					  catch (Throwable th) {}
					  progressBar.setSelection(i);					  
    			  
    			  try {  			    				  
        		  
    				  if (files != null && files.length > 0) {
    					  label.setText(label.getText()+"\n"+files[i]);  					  
    				  }
    				  
	        		  String hostname = "dev.wb-informatik.ch";
	        		  int port = 80;
	        		  InetAddress addr = InetAddress.getByName(hostname);
	        		  Socket socket = new Socket(addr, port);
	
	        		  // Send header
	        		  String path = "http://dev.wb-informatik.ch/ck/java/upload.php";
	
	        		  // File To Upload
	        		  File theFile = new File(files[i]);
	        		          		 
	        		  System.out.println(files);
	        		  System.out.println(addr.getHostAddress());
	        		  System.out.println ("Dateigrösse: " + (int) theFile.length());
	        		 
	        		  DataInputStream fis = new DataInputStream(new BufferedInputStream(new
	        		  FileInputStream(theFile)));
	        		  byte[] theData = new byte[(int) theFile.length()];
	
	        		  fis.readFully(theData);
	        		  fis.close();

	        		  DataOutputStream raw = new
	        		  DataOutputStream(socket.getOutputStream());
	        		  Writer wr = new OutputStreamWriter(raw);
	        		  
	        		  
	        		  String command =
	        		  "--dill\r\n"
	        		  + "Content-Disposition: form-data; name=\"uploadfile\"; filename=\""
	        		  + theFile.getName() + "\"\r\n"
	        		  + "Content-Type: text/xml\r\n"
	        		  + "\r\n";
	
	        		  String trail = "\r\n--dill--\r\n";
	
	        		  String header =
	        		  "POST "+path+" HTTP/1.0\r\n"
	        		  + "Accept: */*\r\n"
	        		  + "Referer: dev.wb-informatik.ch\r\n"
	        		  + "Accept-Language: de\r\n"
	        		  + "Content-Type: multipart/form-data; boundary=dill\r\n"
	        		  + "User_Agent: \r\n"
	        		  + "Host: dev.wb-informatik.ch\r\n"
	        		  + "Content-Length: " + ((int) theFile.length() + command.length() +
	        		  trail.length()) + "\r\n"
	        		  //+ "Connection: Keep-Alive\r\n"
	        		  + "Pragma: no-cache\r\n"
	        		  + "\r\n";        		  

	        		  wr.write(header);
	        		  wr.write(command);
	
	        		  wr.flush();
	        		  raw.write(theData);
	        		  raw.flush( );
	        		  wr.write("\r\n--dill--\r\n");
	        		  wr.flush( );
	
	        		  BufferedReader rd = new BufferedReader(new
	        		  InputStreamReader(socket.getInputStream()));
	        		  
	        		  String line;
	        		  while ((line = rd.readLine()) != null) {
	        		  System.out.println(line);
	        		  }
	        		  wr.close();
	        		  raw.close();
	        		  	
	        		  socket.close();        		  
        	  	}
        	  	    			  
    			  catch (Exception e) { System.out.println(e.toString()); }
    		  }

    		  label.setText("Upload abgeschlossen!");
    		  progressBar.setSelection(0);
    	  }
      }	
    });
    
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}

und der PHP:
PHP:
<?php
//error_reporting(E_ALL);
//phpinfo(INFO_VARIABLES);

$uploadfile = $_POST['uploadfile'];
var_dump($_POST);

$maxfilesize = 10000; //kByte
	if ($_FILES['uploadfile']['size'] < $maxfilesize*1024){
	
		echo '<pre>';
		if(!move_uploaded_file($_FILES['uploadfile']['tmp_name'],$_FILES['uploadfile']['name']))
		{
		echo "Possible file upload attack or no file specified!\n";
		}
	echo 'Here is some more debugging info:';
	
	print_r($_FILES);
	print "</pre>";
	
	}
	
	else {
		echo "File exceeds maximum filesize: $maxfilesize kByte";
	}

	
?>

<html>
<body>
<form enctype="multipart/form-data" action="<?php $_SERVER['PHP_SELF']?>" method="post">
Datei: <input name="uploadfile" type="file" size="25">
<p><input type="submit" value="senden">
</form>
</body>
</html>
 

Neue Beiträge

Zurück