我正在开发客户端/服务器程序上的 Swing 应用程序,用于通过 Java 套接字发送不同类型的文件。我已使用以下链接作为参考:Java 中的文件传输
我已根据我的要求修改了代码,如下所示:
public File file;
public String fileName=new String();
String path=new String();
服务器端代码:
try
{
File file=new File(path);
ServerSocket servsock = new ServerSocket(12345);
Socket sockFileName=servsock.accept();
FileOutputStream fout=new FileOutputStream(path);
DataOutputStream dOut=new DataOutputStream(sockFileName.getOutputStream());
dOut.writeUTF(fileName);
dOut.flush();
dOut.close();
sockFileName.close();
servsock.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(mainPanel, "file name: "+e);
}
try
{
ServerSocket servsock=new ServerSocket(12345);
Socket sock = servsock.accept();
byte[] mybytearray = new byte[(int) file.length()];
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
bis.read(mybytearray, 0, mybytearray.length);
OutputStream os = sock.getOutputStream();
os.write(mybytearray, 0, mybytearray.length);
os.flush();
os.close();
sock.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(mainPanel, "file sending: "+e);
}
客户端代码:
try
{
Socket sock = new Socket("127.0.0.1", 12345);
DataInputStream dIn=new DataInputStream(sock.getInputStream());
fileName=dIn.readUTF();
jLabel3.setText(fileName);
dIn.close();
sock.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(mainPanel, "file name: "+e);
}
JOptionPane.showMessageDialog(mainPanel, "file is being recieved . . . ");
try
{
Socket sock = new Socket("127.0.0.1", 12345);
byte[] mybytearray = new byte[1024];
System.out.println("1");
InputStream is = sock.getInputStream();
System.out.println("2");
File recievedFile=new File("Z:\\"+fileName);
System.out.println("3");
recievedFile.createNewFile();
System.out.println("4");
FileOutputStream fos = new FileOutputStream(recievedFile);
System.out.println("5");
BufferedOutputStream bos = new BufferedOutputStream(fos);
System.out.println("6");
int bytesRead = is.read(mybytearray, 0, mybytearray.length);
System.out.println("7");
bos.write(mybytearray, 0, bytesRead);
System.out.println("8");
bos.flush();
System.out.println("9");
bos.close();
sock.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(mainPanel, "file sending: "+e);
}
我真的无法解决为什么我在客户端收到文件ArrayOutOfBoundException
时bos.write(mybytearray, 0, bytesRead);
在线。
编辑:
代码结构有点复杂,但我敢打赌,你一定能找到函数。实际上客户端和服务器都使用不同的按钮在同一个窗口上实现。
package samplefilesendrecieveclientserver;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import javax.jws.WebService;
import javax.net.ssl.SSLServerSocket;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
/**
* The application's main frame.
*/
public class SampleFileSendRecieveClientServerView extends FrameView {
public SampleFileSendRecieveClientServerView(SingleFrameApplication app) {
super(app);
initComponents();
// status bar initialization - message timeout, idle icon and busy animation, etc
ResourceMap resourceMap = getResourceMap();
int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
messageTimer = new Timer(messageTimeout, new ActionListener() {
public void actionPerformed(ActionEvent e) {
statusMessageLabel.setText("");
}
});
messageTimer.setRepeats(false);
int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
for (int i = 0; i < busyIcons.length; i++) {
busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
}
busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
public void actionPerformed(ActionEvent e) {
busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
}
});
idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
statusAnimationLabel.setIcon(idleIcon);
progressBar.setVisible(false);
// connecting action tasks to status bar via TaskMonitor
TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
public void propertyChange(java.beans.PropertyChangeEvent evt) {
String propertyName = evt.getPropertyName();
if ("started".equals(propertyName)) {
if (!busyIconTimer.isRunning()) {
statusAnimationLabel.setIcon(busyIcons[0]);
busyIconIndex = 0;
busyIconTimer.start();
}
progressBar.setVisible(true);
progressBar.setIndeterminate(true);
} else if ("done".equals(propertyName)) {
busyIconTimer.stop();
statusAnimationLabel.setIcon(idleIcon);
progressBar.setVisible(false);
progressBar.setValue(0);
} else if ("message".equals(propertyName)) {
String text = (String)(evt.getNewValue());
statusMessageLabel.setText((text == null) ? "" : text);
messageTimer.restart();
} else if ("progress".equals(propertyName)) {
int value = (Integer)(evt.getNewValue());
progressBar.setVisible(true);
progressBar.setIndeterminate(false);
progressBar.setValue(value);
}
}
});
}
@Action
public void showAboutBox() {
if (aboutBox == null) {
JFrame mainFrame = SampleFileSendRecieveClientServerApp.getApplication().getMainFrame();
aboutBox = new SampleFileSendRecieveClientServerAboutBox(mainFrame);
aboutBox.setLocationRelativeTo(mainFrame);
}
SampleFileSendRecieveClientServerApp.getApplication().show(aboutBox);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
mainPanel = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jButton2 = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
jButton3 = new javax.swing.JButton();
jLabel3 = new javax.swing.JLabel();
menuBar = new javax.swing.JMenuBar();
javax.swing.JMenu fileMenu = new javax.swing.JMenu();
javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
javax.swing.JMenu helpMenu = new javax.swing.JMenu();
javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
statusPanel = new javax.swing.JPanel();
javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
statusMessageLabel = new javax.swing.JLabel();
statusAnimationLabel = new javax.swing.JLabel();
progressBar = new javax.swing.JProgressBar();
mainPanel.setName("mainPanel"); // NOI18N
org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(samplefilesendrecieveclientserver.SampleFileSendRecieveClientServerApp.class).getContext().getResourceMap(SampleFileSendRecieveClientServerView.class);
jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
jButton1.setName("jButton1"); // NOI18N
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
jButton1MousePressed(evt);
}
});
jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
jLabel1.setName("jLabel1"); // NOI18N
jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
jButton2.setName("jButton2"); // NOI18N
jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
jButton2MousePressed(evt);
}
});
jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
jLabel2.setName("jLabel2"); // NOI18N
jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N
jButton3.setName("jButton3"); // NOI18N
jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
jButton3MousePressed(evt);
}
public void mouseReleased(java.awt.event.MouseEvent evt) {
jButton3MouseReleased(evt);
}
});
jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
jLabel3.setName("jLabel3"); // NOI18N
javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
mainPanel.setLayout(mainPanelLayout);
mainPanelLayout.setHorizontalGroup(
mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(mainPanelLayout.createSequentialGroup()
.addGap(77, 77, 77)
.addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton3)
.addGroup(mainPanelLayout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(mainPanelLayout.createSequentialGroup()
.addComponent(jButton2)
.addGap(84, 84, 84)
.addComponent(jButton1))
.addComponent(jLabel1))
.addContainerGap(96, Short.MAX_VALUE))
);
mainPanelLayout.setVerticalGroup(
mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
.addGap(23, 23, 23)
.addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jLabel3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 90, Short.MAX_VALUE)
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton2)
.addComponent(jButton1))
.addGap(43, 43, 43))
);
menuBar.setName("menuBar"); // NOI18N
fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
fileMenu.setName("fileMenu"); // NOI18N
javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(samplefilesendrecieveclientserver.SampleFileSendRecieveClientServerApp.class).getContext().getActionMap(SampleFileSendRecieveClientServerView.class, this);
exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
exitMenuItem.setName("exitMenuItem"); // NOI18N
fileMenu.add(exitMenuItem);
menuBar.add(fileMenu);
helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
helpMenu.setName("helpMenu"); // NOI18N
aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
aboutMenuItem.setName("aboutMenuItem"); // NOI18N
helpMenu.add(aboutMenuItem);
menuBar.add(helpMenu);
statusPanel.setName("statusPanel"); // NOI18N
statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N
statusMessageLabel.setName("statusMessageLabel"); // NOI18N
statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N
progressBar.setName("progressBar"); // NOI18N
javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
statusPanel.setLayout(statusPanelLayout);
statusPanelLayout.setHorizontalGroup(
statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
.addGroup(statusPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(statusMessageLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 230, Short.MAX_VALUE)
.addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(statusAnimationLabel)
.addContainerGap())
);
statusPanelLayout.setVerticalGroup(
statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(statusPanelLayout.createSequentialGroup()
.addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(statusMessageLabel)
.addComponent(statusAnimationLabel)
.addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(3, 3, 3))
);
setComponent(mainPanel);
setMenuBar(menuBar);
setStatusBar(statusPanel);
}// </editor-fold>
public File file;
public String fileName=new String();
String path=new String();
private void jButton2MousePressed(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
JFileChooser chooser=new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.showSaveDialog(null);
path=chooser.getSelectedFile().getAbsolutePath();
fileName=chooser.getSelectedFile().getName();
jLabel1.setText(path);
file=new File(path);
}
private void jButton1MousePressed(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
try
{
File file=new File(path);
ServerSocket servsock = new ServerSocket(12345);
Socket sockFileName=servsock.accept();
FileOutputStream fout=new FileOutputStream(path);
DataOutputStream dOut=new DataOutputStream(sockFileName.getOutputStream());
dOut.writeUTF(fileName);
dOut.flush();
dOut.close();
sockFileName.close();
servsock.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(mainPanel, "file name: "+e);
}
try
{
ServerSocket servsock=new ServerSocket(12345);
Socket sock = servsock.accept();
byte[] mybytearray = new byte[(int) file.length()];
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
bis.read(mybytearray, 0, mybytearray.length);
OutputStream os = sock.getOutputStream();
os.write(mybytearray, 0, mybytearray.length);
os.flush();
os.close();
sock.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(mainPanel, "file sending: "+e);
}
}
private void jButton3MouseReleased(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
}
private void jButton3MousePressed(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
try
{
Socket sock = new Socket("127.0.0.1", 12345);
DataInputStream dIn=new DataInputStream(sock.getInputStream());
fileName=dIn.readUTF();
jLabel3.setText(fileName);
dIn.close();
sock.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(mainPanel, "file name: "+e);
}
JOptionPane.showMessageDialog(mainPanel, "file is being recieved . . . ");
try
{
Socket sock = new Socket("127.0.0.1", 12345);
byte[] mybytearray = new byte[1024000];
System.out.println("1");
InputStream is = sock.getInputStream();
System.out.println("2");
File recievedFile=new File("Z:\\"+fileName);
System.out.println("3");
recievedFile.createNewFile();
System.out.println("4");
FileOutputStream fos = new FileOutputStream(recievedFile);
System.out.println("5");
BufferedOutputStream bos = new BufferedOutputStream(fos);
System.out.println("6");
int bytesRead = is.read(mybytearray, 0, mybytearray.length);
System.out.println("7");
bos.write(mybytearray, 0, bytesRead);
System.out.println("8");
bos.flush();
System.out.println("9");
bos.close();
sock.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(mainPanel, "file sending: "+e);
}
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JPanel mainPanel;
private javax.swing.JMenuBar menuBar;
private javax.swing.JProgressBar progressBar;
private javax.swing.JLabel statusAnimationLabel;
private javax.swing.JLabel statusMessageLabel;
private javax.swing.JPanel statusPanel;
// End of variables declaration
private final Timer messageTimer;
private final Timer busyIconTimer;
private final Icon idleIcon;
private final Icon[] busyIcons = new Icon[15];
private int busyIconIndex = 0;
private JDialog aboutBox;
}