我对文件传输系统有一点问题,以前从未发生过。
@Override
public void run()
{
while (true)
{
try
{
String message = (String) new ObjectInputStream(socket.getInputStream()).readObject();
if (message.startsWith("message="))
{
message = message.replaceFirst("message=", "");
frame.addLine(message);
}
else if (message.startsWith("file="))
{
final String file = message.replaceFirst("file=", "");
int selection = JOptionPane.showOptionDialog(frame, "Do you want to download \"" + file + "\"?", "Download", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, JOptionPane.YES_OPTION);
if (selection == JOptionPane.YES_OPTION)
{
JFileChooser jFileChooser = new JFileChooser();
jFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int rValue = jFileChooser.showDialog(frame, "Öffnen");
if (rValue == JFileChooser.APPROVE_OPTION)
{
new ObjectOutputStream(socket.getOutputStream()).writeObject("file_confirm=" + file);
String selectedDir = jFileChooser.getSelectedFile().getAbsolutePath();
OutputStream out = new FileOutputStream(new File(selectedDir, file));
try
{
byte[] buffer = new byte[1024];
for (int c = 0; (c = socket.getInputStream().read(buffer)) > 0;)
{
out.write(buffer, 0, c);
System.out.println(c);
}
System.out.println("test1");
}
catch (Exception ex)
{
ex.printStackTrace();
}
finally
{
System.out.println("test2");
out.flush();
out.close();
System.out.println("test3");
}
}
}
}
else if (message.startsWith("kick()"))
{
frame.setVisible(false);
frame.dispose();
JOptionPane.showMessageDialog(frame, "You have been kicked!", "Error", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
else if (message.startsWith("clear()"))
{
frame.clear();
}
}
catch (Exception ex)
{
ex.printStackTrace();
frame.setVisible(false);
frame.dispose();
JOptionPane.showMessageDialog(frame, "Connection timed out!", "Error", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}
}
打印出来(文件大小为31 KB):
大约30倍1024(正如预期的那样)
382
4
10
4
10
4
10
----- 4和10一直持续到我终止应用程序-----
无论我使用什么文件,它总是重复这个4和10。所以我自己发现了问题,它陷入了for-loop,但为什么呢?
如果您遇到同样的问题,我只想告诉您有两种解决方法。
一个简单的
if(c
也会修复它
答案 0 :(得分:0)
如果你想从单独的线程中摆动操作,你就可以使线程安全。使其线程安全使用
EventQueue.invokeLater(new Runnable() {
public void run() {
//put your swing operation here
}
});
EventQueue.invokeLater(new Runnable() {
public void run() {
int selection = JOptionPane.showOptionDialog(frame,
"Do you want to download \"" + file + "\"?",
"Download", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, null,
JOptionPane.YES_OPTION);
if (selection == JOptionPane.YES_OPTION) {
JFileChooser jFileChooser = new JFileChooser();
jFileChooser
.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int rValue = jFileChooser.showDialog(frame, "Öffnen");
if (rValue == JFileChooser.APPROVE_OPTION) {
new ObjectOutputStream(socket.getOutputStream())
.writeObject("file_confirm=" + file);
String selectedDir = jFileChooser.getSelectedFile()
.getAbsolutePath();
OutputStream out = new FileOutputStream(new File(
selectedDir, file));
try {
byte[] buffer = new byte[1024];
for (int c = 0; (c = socket.getInputStream()
.read(buffer)) > 0;) {
out.write(buffer, 0, c);
System.out.println(c);
}
System.out.println("test1");
} catch (Exception ex) {
ex.printStackTrace();
} finally {
System.out.println("test2");
out.flush();
out.close();
System.out.println("test3");
}
}
}//
}
});
其他挥杆操作相同