Moin,
da bin ich auch schon wieder. Ich hab mich nu ne ganze weile mit dem JProgressBar versucht um diesen in mein Applet einzubinden.
Leider passiert da nicht viel, es wird mir nur die ProgressBar angezeigt, aber kein Vortschritt. Kann mir jemand weiter helfen?
Dabei geht es hauptsächlich um die Methoden runApplet, dort wird der Thread gestartet, sowie run, writeFileContent und StreamCopy.
Sorry für den gerade ein wenig unstruckturierten Code ^^, aber wenn ich am rumprobieren bin hack ich einfach drauf los...
Danke im vorraus!
Gruß!
da bin ich auch schon wieder. Ich hab mich nu ne ganze weile mit dem JProgressBar versucht um diesen in mein Applet einzubinden.
Leider passiert da nicht viel, es wird mir nur die ProgressBar angezeigt, aber kein Vortschritt. Kann mir jemand weiter helfen?
Dabei geht es hauptsächlich um die Methoden runApplet, dort wird der Thread gestartet, sowie run, writeFileContent und StreamCopy.
Java:
@SuppressWarnings("unchecked")
public void runApplet(String siteID, String containerID,
String UploadDirectory, String updatenoderf, String filename,
String description, String majorversion, String overwrite,
String thumbnails) {
this.siteID = siteID;
this.containerID = containerID;
this.uploadDirectory = UploadDirectory;
this.updatenoderef = updatenoderf;
this.filename = filename;
this.description = description;
this.majorversion = majorversion;
this.overwrite = overwrite;
this.thumbnails = thumbnails;
// Prüft ob ein File-Upload stattfindet.
if (this.thumbnails != null) {
fileUpload = true;
}
// Läd gesuchte Datei.
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
loadFile();
return null;
}
});
// Setzt den Namen der Datei die hochgeladen werden soll(Nur bei
// FileUpload).
if (fileUpload == true) {
this.filename = filedata.getName();
this.updatenoderef = "";
}
this.contenttype = getMimeType();
pb = new JProgressBar();
Panel panel = new Panel();
panel.add(pb);
this.add(panel);
this.setVisible(true);
new Thread( this ).start();
}
@SuppressWarnings("unchecked")
public void run(){
try {
// Öffnet eine Verbindung zum Server.
openConnection();
// Schreibt die Request.
writeRequest();
// Bekommt eine Antwort vom Server (Testzwecke!).
getResponseFromServer();
// Trennt die Verbindung zum Server.
disconnectFromServer();
// Löscht Datei vom lokalem System.
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
deleteFile();
return null;
}
});
// Setzt default-Werte
setDefault();
} catch (Exception e) {
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
public void loadFile() {
File files = (File) AccessController
.doPrivileged(new PrivilegedAction() {
public Object run() {
return new File(localFileBase);
}
});
boolean fileFound = false;
if (filename != null) {
// Sucht nach Datei im lokalen Verzeichniss.
for (File file : files.listFiles()) {
if (file.getName().equals(filename)) {
filedata = file;
fileFound = true;
}
}
}
// Falls keine Datei im Standartverzeichnis liegt wird der File-Browser
// geöffnet.
if (fileFound == false) {
JFileChooser chooser = new JFileChooser();
chooser.addChoosableFileFilter(new FileFilter() {
public boolean accept(File f) {
if (f.isDirectory())
return true;
if (fileUpload == false) {
return f.getName().endsWith(getFileType());
}
return f.isFile();
}
public String getDescription() {
if (fileUpload == false) {
return "*" + getFileType();
}
return "*allFiles";
}
});
int returnVal = chooser.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
filedata = chooser.getSelectedFile();
}
}
}
public void openConnection() throws IOException {
// Öffnet URL-Connection zum Server.
URL url = new URL(uploadURL);
// Öffnet HTTP-Connection zum Server.
conn = (HttpURLConnection) url.openConnection();
// Erlaubt Eingaben.
conn.setDoInput(true);
// Erlaubt Ausgaben
conn.setDoOutput(true);
// cached Copy wird nicht gesetzt.
conn.setUseCaches(false);
// POST-Methode soll genutzt werden.
conn.setRequestMethod("POST");
// HTTP-Header wird initialisiert.
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type",
"multipart/form-data; boundary=" + boundary);
System.out.print(conn.getRequestProperties());
}
public void writeRequest() throws IOException {
// Zum schreiben der Daten.
os = conn.getOutputStream();
dos = new DataOutputStream(os);
writeLine(lineSep + lineSep);
writeLine(twoStrings + boundary);
writeLine(lineSep);
writeLine("Content-Disposition: form-data; name=\"siteId\"" + lineSep
+ lineSep);
writeLine(siteID + lineSep);
writeLine(twoStrings + boundary + lineSep);
writeLine("Content-Disposition: form-data; name=\"containerId\""
+ lineSep + lineSep);
writeLine(containerID + lineSep);
writeLine(twoStrings + boundary + lineSep);
writeLine("Content-Disposition: form-data; name=\"destination\""
+ lineSep + lineSep);
writeLine("" + lineSep);
writeLine(twoStrings + boundary + lineSep);
writeLine("Content-Disposition: form-data; name=\"username\"" + lineSep
+ lineSep);
writeLine("" + lineSep);
writeLine(twoStrings + boundary + lineSep);
writeLine("Content-Disposition: form-data; name=\"updateNodeRef\""
+ lineSep + lineSep);
writeLine(updatenoderef + lineSep);
writeLine(twoStrings + boundary + lineSep);
writeLine("Content-Disposition: form-data; name=\"uploadDirectory\""
+ lineSep + lineSep);
writeLine(uploadDirectory + lineSep);
writeLine(twoStrings + boundary + lineSep);
writeLine("Content-Disposition: form-data; name=\"overwrite\""
+ lineSep + lineSep);
writeLine(overwrite + lineSep);
writeLine(twoStrings + boundary + lineSep);
writeLine("Content-Disposition: form-data; name=\"thumbnails\""
+ lineSep + lineSep);
writeLine(thumbnails + lineSep);
writeLine(twoStrings + boundary + lineSep);
writeLine("Content-Disposition: form-data; name=\"contentType\""
+ lineSep + lineSep);
writeLine("cm:content" + lineSep);
writeLine(twoStrings + boundary + lineSep);
writeLine("Content-Disposition: form-data; name=\"filedata\"; filename="
+ "\"" + filedata.getName() + "\"" + lineSep);
writeLine("Content-Type: " + contenttype + lineSep + lineSep);
try {
// Schreibt den Dateieninhalt in die Request
writeFileContent();
} catch (PrivilegedActionException e) {
e.printStackTrace();
}
writeLine(lineSep);
writeLine(twoStrings + boundary + lineSep);
writeLine("Content-Disposition: form-data; name=\"majorVersion\""
+ lineSep + lineSep);
writeLine(majorversion + lineSep);
writeLine(twoStrings + boundary + lineSep);
writeLine("Content-Disposition: form-data; name=\"description\""
+ lineSep + lineSep);
writeLine(description + lineSep);
writeLine(twoStrings + boundary + twoStrings);
// Stream beenden.
dos.flush();
dos.close();
}
@SuppressWarnings("unchecked")
public void writeFileContent() throws IOException,
PrivilegedActionException {
// Stream zu Auslesen der Daten.
fileInputStream = (FileInputStream) AccessController
.doPrivileged(new PrivilegedExceptionAction() {
public Object run() throws FileNotFoundException {
return new FileInputStream(filedata);
}
});
// streamCopy(fileInputStream, dos);
streamCopy();
}
public void streamCopy() throws IOException {
int begin = 0;
int end = (int) filedata.length();
int current = begin;
setMinMax(begin, end);
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
// long count = 0;
int n = 0;
try {
while (-1 != (n = fileInputStream.read(buffer))) {
try {
dos.write(buffer, 0, n);
setValue(current++);
} catch (IOException e) {
e.printStackTrace();
}
// count += n;
}
} catch (IOException e) {
e.printStackTrace();
}
// return count;
}
private void setMinMax(final int min, final int max) {
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
pb.setMinimum(min);
pb.setValue(min);
pb.setMaximum(max);
}
});
} catch (Exception e) {
}
}
private void setValue(final int value) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
pb.setValue(value);
} catch (NullPointerException ex) {
// silent exception
}
}
});
}
private void writeLine(String text) throws IOException {
System.out.print(text);
dos.writeBytes(text);
}
public void getResponseFromServer() {
try {
// JOptionPane.showMessageDialog(this, conn.getResponseCode());
System.out.println(conn.getResponseCode());
// streamCopy(conn.getInputStream(), System.out);
} catch (Exception ioex) {
JOptionPane.showMessageDialog(this, ioex, "ServerResponse",
JOptionPane.ERROR_MESSAGE);
System.out.print("\nFrom (ServerResponse): " + ioex);
}
}
public void disconnectFromServer() {
conn.disconnect();
}
public void deleteFile() {
File files = new File(localFileBase);
for (File file : files.listFiles()) {
if (file.getName().equals(filename)) {
file.delete();
}
}
}
public void setDefault() {
this.filedata = null;
this.siteID = null;
this.containerID = null;
this.uploadDirectory = null;
this.updatenoderef = null;
this.filename = null;
this.description = null;
this.contenttype = null;
this.majorversion = null;
this.overwrite = null;
this.thumbnails = null;
this.fileUpload = false;
}
protected static String randomString() {
random = new Random();
return Long.toString(random.nextLong(), 36);
}
protected String getMimeType() {
FileNameMap fileNameMap = URLConnection.getFileNameMap();
return fileNameMap.getContentTypeFor(filename);
}
protected String getFileType() {
int indexOf = filename.lastIndexOf(".");
return filename.substring(indexOf);
}
}
Sorry für den gerade ein wenig unstruckturierten Code ^^, aber wenn ich am rumprobieren bin hack ich einfach drauf los...
Danke im vorraus!
Gruß!