Downloadproblem
-
Hallo, ich weis, man wird hier meistens gleich mit faulen Eiern und Tomaten beworfen, wenn man mal einen (fast) kompletten code, postet. Aber ich weis in diesem Fall nun wirklich nict wo der Fehler liegt:
import javax.swing.*; import java.util.StringTokenizer; import java.util.LinkedList; import java.io.*; import java.net.*; import java.awt.*; import java.awt.event.*; public class Gic extends Frame implements ActionListener { ServerSocket server; int port = 0; String host = ""; String outputFile = ""; TextArea output = new TextArea("", 80, 80); InputConsole input; boolean run = true; String lastCommand = ""; LinkedList scriptList = new LinkedList(); String site = "BLANK"; List files = new List(); Download[] downloads; boolean[] visible = new boolean[6]; int v = 0; Panel center = new Panel(new BorderLayout()); int progress = 0; JProgressBar progressBar = new JProgressBar(JProgressBar.HORIZONTAL, 0, 1000); JLabel volume = new JLabel("Runtergeladene Bytes: "); boolean abort = false; Panel north; public Gic() { super("Gic"); setSize(400, 400); setLayout(new BorderLayout()); center.add("Center", output); add("Center", center); output.setEditable(false); files.addActionListener(this); input = new InputConsole("", 80, this); add("South", input); input.setBackground(Color.black); input.setForeground(Color.white); input.requestFocus(); north = new Panel(new GridLayout(2, 1)); north.add(volume); north.add(progressBar); add("North", north); show(); } public final void actionPerformed(ActionEvent e) { Object obj = e.getSource(); if (obj instanceof List) { int[] s = files.getSelectedIndexes(); Download dl = downloads[s[0]]; DownloadViewer d = dl.getViewer(); Point p = getLocationOnScreen(); if (visible[0] == false) { d.setLocation(p.x+getWidth(), p.y); d.setVisible(true); visible[0] = true; } else if (visible[1] == false) { d.setLocation(p.x+getWidth()+200, p.y); d.setVisible(true); visible[1] = true; } else if (visible[2] == false) { d.setLocation(p.x+getWidth(), p.y+100); d.setVisible(true); visible[2] = true; } else if (visible[3] == false) { d.setLocation(p.x+getWidth()+200, p.y+100); d.setVisible(true); visible[3] = true; } else if (visible[4] == false) { d.setLocation(p.x+getWidth(), p.y+200); d.setVisible(true); visible[4] = true; } else if (visible[5] == false) { d.setLocation(p.x+getWidth()+200, p.y+200); d.setVisible(true); visible[5] = true; } else { // kein weiterer Download m”gl., Beendigung abwarten } } } public final void processRequest() { if (!input.getText().equals("repeat")) lastCommand = input.getText(); progress = 0; progressBar.setValue(0); v = 0; volume.setText("Runtergeladene Bytes: 0"); String cmd = input.getText(); if (cmd.indexOf("setPort") != -1) { StringTokenizer s = new StringTokenizer(cmd, " "); String str = s.nextToken(); str = s.nextToken(); port = Integer.parseInt(str); output.append("\nset port to: " + port + "\n"); } else if (cmd.indexOf("setHost") != -1) { StringTokenizer s = new StringTokenizer(cmd, " "); host = s.nextToken(); host = s.nextToken(); output.append("\nset host to: " + host + "\n"); } else if (cmd.equalsIgnoreCase("QUIT")) { output.append("\nProgramm wird beendet..."); setVisible(false); dispose(); System.exit(0); } else if (cmd.indexOf("setOutputFile") != -1) { StringTokenizer str = new StringTokenizer(input.getText(), " "); outputFile = str.nextToken(); outputFile = str.nextToken(); output.append("\nset output file to: " + outputFile + "\n"); } else if (cmd.indexOf("getIP") != -1) { try { StringTokenizer str = new StringTokenizer(cmd, " "); String ina = str.nextToken(); ina = str.nextToken(); InetAddress addi = InetAddress.getByName(ina); output.append(new String("\nDie IP-Adresse von \"" + addi.getHostName() + "\" lautet: \"" + addi.getHostAddress() + "\"\n")); } catch (UnknownHostException e) { output.append("\n" + e.toString() + "\n"); } } else if (cmd.equals("repeat")) { input.setText(lastCommand); processRequest(); } else if (cmd.indexOf("doScript") != -1) { try { StringTokenizer str = new StringTokenizer(cmd, " "); String file = str.nextToken(); file = str.nextToken(); BufferedReader ir = new BufferedReader(new FileReader(file)); while ((file = ir.readLine()) != null) { scriptList.add(new String(file)); } ir.close(); } catch (Exception e) { System.err.println(e.toString()); } doScript(); } else if (cmd.indexOf("download") != -1) { ************************************** /*Download f = new Download(from, to, this); f.setVisible(true); */ ************************************** initFiles(); if (files.getItemCount() == 0) { output.append("Keine gueltigen URLs gefunden"); } else { downloads = new Download[files.getItemCount()]; for (int i = 0; i < files.getItemCount(); ++i) { String from = files.getItem(i); String to = ""; StringTokenizer str = new StringTokenizer(from, "/"); while (str.hasMoreTokens()) to = str.nextToken(); downloads[i] = new Download(from, to, this); } setVisible(false); remove(center); center.remove(output); GridBagLayout gbl = new GridBagLayout(); center.setLayout(gbl); GridBagConstraints gbc; gbc = createGbc(0, 0, 1, 4); gbc.fill = GridBagConstraints.BOTH; gbc.weightx = 100; gbc.weighty = 100; gbl.setConstraints(files, gbc); center.add(files); gbc = createGbc(1, 0, 3, 4); gbc.fill = GridBagConstraints.BOTH; gbc.weightx = 100; gbc.weighty = 100; gbl.setConstraints(output, gbc); center.add(output); add("Center", center); setSize(800, 600); setVisible(true); } } else { try { site = null; Socket socket = new Socket(host, port); socket.setSoTimeout(10000); PrintStream out = new PrintStream(socket.getOutputStream()); out.print(cmd + "\r\n"); BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); String line; while ((line = in.readLine()) != null) { if (progress == 1000) progress = 0; ++progress; progressBar.setValue(progress); output.append(line + "\r\n"); site += (line + "\r\n"); v += line.length(); } if (outputFile.length() >= 3 && output != null) { BufferedWriter w = new BufferedWriter(new FileWriter(outputFile, false)); w.write(output.getText(), 0, output.getText().length()); w.close(); } in.close(); out.close(); progress = 0; progressBar.setValue(1000); socket.close(); } catch (IOException e) { output.append("\n" + e.toString() + "\n"); } } } private final GridBagConstraints createGbc(int x, int y, int width, int height) { GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = x; gbc.gridy = y; gbc.gridwidth = width; gbc.gridheight = height; gbc.insets = new Insets(1, 1, 1, 1); return gbc; } public final void doScript() { for (int i = 0; i < scriptList.size(); i++) { input.setText((String)scriptList.get(i)); output.append("\n"+input+"\n"); processRequest(); } } private final void initFiles() { if (site.equals("BLANK") || site == null) return; StringTokenizer str = new StringTokenizer(site, "<>"); while (str.hasMoreTokens()) { String s = str.nextToken(); if (s.startsWith("A HREF") || s.startsWith("a href")) { StringTokenizer str2 = new StringTokenizer(s, "="); s = str2.nextToken(); s = str2.nextToken(); if (s.indexOf("\"") != -1) { s = s.substring(s.indexOf("\"")+1, s.indexOf("\"", s.indexOf("\"")+1)); } else { s = s.substring(0, s.indexOf(" ")+1); } files.add(s.trim()); } } } public final static void main(String[] args) { /* Gic g = new Gic(); System.out.println("\n***Gapp Internet Communicator***\n\nWarte auf Anweisungen...\n"); while (g.run) { try { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); //while (!in.ready()); g.input = in.readLine(); if (!g.input.equals("repeat")) g.lastCommand = g.input; } catch (IOException e) { System.err.println(e.toString()); } g.processRequest(); if (g.output != null) System.out.println("\n"+g.output); g.output = null; } */ Gic g = new Gic(); } }
Hi, also wenn ich statt dem code der unter dem sternchenumrandeten steht(in processRequest), den sternchenumrandeten code nich kommentiere dann klappt das! Meine Klasse Download lädt ordnungsgemäß die gewünschten Datein runter und speichert sie. In meinem Beispiel ein video von 1.2 mb Größe. Das konnte ich auch 10mal hinschreiben, also 10 Downloads separat starten und es hat alles einwandfrei geklappt. Wenn ich es aber nun so mache, wie ich´s halt mache, also mögl. Downloads auflisten, wählen, anzeigen und runterladen auf Knopfdruck, dann kriege ich immer nur 642 Bytes vom Server geschickt. ICH HABE KEINE AHNUNG WARUM!!! Kann mir evtl. jemand helfen?