2013-07-31 3 views
1

Я работаю над качающимся приложением в клиент-серверной программе для отправки различных типов файлов через java-сокеты. Я использовал следующую ссылку для справки: File Transfer in JavaИсключение ArrayIndexOutOfBounds в программе java socket

Я изменил код в соответствии с моими требованиями, как показано ниже:

public File file; 
public String fileName=new String(); 
String path=new String(); 

Server Side Код:

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); на стороне клиента, пока он получает файл.

Edit:

код немного сложную структуру, но я уверен, вы сможете найти функции. На самом деле клиент и сервер реализуются в одном окне с помощью разных кнопок.

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; 
} 

ответ

3

Возможно, ваш файл больше 1024 байтов? Это больше, чем буфер

Edit: Ошибка из-за этой линии:

FileOutputStream fout=new FileOutputStream(path); 

Комментарий это потому, что вы на самом деле не делать ничего с ним. Это код serveride, один из первых строк в вашей первой попытке.

Эта строка открывает выходной поток в ваш файл. Ничего не происходит с этим потоком, но он также не закрыт.

Это вызывает не только ошибку на стороне клиента (поскольку данные не передаются), но и уничтожает все данные, хранящиеся в вашем файле.

Edit 2:

Я попробовал картину с bytesize из 305564

и хотя буфер на стороне клиента был достаточно большим, я только что получил 65536 байт данных, передаваемых одновременно.

Для больших файлов, которые вы должны использовать циклы, чтобы получить все необходимые данные, заполненные в свой буфер:

Я проверил это, потому что я хотел, чтобы получить его работу тоже.

Client Side

try 
       { 
        Socket sock = new Socket("127.0.0.1", 12345); 
        byte[] mybytearray = new byte[65536]; 
        System.out.println("1"); 
        InputStream is = sock.getInputStream(); 
        System.out.println("2"); 
        File recievedFile=new File(fileName+"_res.jpg"); 
        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 loopcount = is.read(); 

        for(int i = 0; i < loopcount; i++) 
        { 
         int bytesRead = is.read(mybytearray, 0, mybytearray.length); 
         System.out.println(bytesRead); 
         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) 
       { 
        e.printStackTrace(); 
       } 

Server Side

try 
      {    
       ServerSocket servsock=new ServerSocket(12345); 
       Socket sock = servsock.accept(); 
        int bufferSize = 65536; 
        byte[] mybytearray = new byte[bufferSize]; 
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); 
        OutputStream os = sock.getOutputStream(); 
        int loopcount = (int) (file.length()/bufferSize)+(file.length()%bufferSize==0?0:1); 
        System.out.println("loopcount: "+loopcount); 
        os.write(loopcount); 
        os.flush(); 
        for(int i = 0; i < loopcount; i ++) 
        { 
         System.out.println(i); 
        bis.read(mybytearray, 0, mybytearray.length); 

        //System.out.println(mybytearray.length); 
        os.write(mybytearray, 0, mybytearray.length); 
        os.flush(); 
        } 

        os.close(); 
        sock.close(); 
       } 
       catch(Exception e) 
       { 
        e.printStackTrace(); 
       } 
+0

я Allready попытался это, но пока безуспешно. Не могли бы вы преувеличивать эту часть, потому что я не могу ее расшифровать. –

+0

Можете ли вы опубликовать все классы для клиентов и серверов? Или, по крайней мере, верхние функции внутри тела функции? Поэтому я могу проверить это. – Loki

+0

У вас есть код? –

2

InputStream.read()returns -1 в конце потока. Вы передаете это bos.write().

int bytesRead = is.read(mybytearray, 0, mybytearray.length); 
System.out.println("7"); 
bos.write(mybytearray, 0, bytesRead); 

Из documentation:

Если выключение имеет отрицательное значение, или Len имеет отрицательное значение, или OFF + Len больше, чем длина массива б, затем IndexOutOfBoundsException выбрасывается.

+0

Хорошо, я получил его сейчас. Это значит, что я не могу положить файл в выходной поток на стороне сервера? –

+0

@NiteshVerma наверное. Я не очень хорошо посмотрел на сервер. Просто при ошибке на стороне клиента (которую вы должны обрабатывать независимо от того, установлен ли сервер или нет). – kiheru

+0

YEs работает нормально в моей системе, но когда я запускаю его в сети, я получаю сообщение об ошибке, соединение отклонено. Я использовал IP = Inet4Address.getLocalHost(). GetHostAddress(); чтобы получить ip системы. Что теперь могло пойти не так? –

1

Модуль сервера

import java.io.BufferedInputStream; 

import java.io.File; 

import java.io.FileInputStream; 

import java.io.IOException; 

import java.io.OutputStream; 

import java.net.ServerSocket; 

import java.net.Socket; 

public class ServerMain { 

    public static void main(String[] args) throws IOException { 

    ServerSocket servsock = new ServerSocket(6789); 

    File myFile = new File("ServerMain.java"); 

    while (true) { 

     Socket sock = servsock.accept(); 

     byte[] mybytearray = new byte[1024]; 

     BufferedInputStream bis = new BufferedInputStream(new FileInputStream(myFile)); 

     OutputStream os = sock.getOutputStream(); 
     int count; 
      while ((count = bis.read(mybytearray)) >= 0) { 
       os.write(mybytearray, 0, count); 

      } 

     os.flush(); 
     sock.close(); 
    } 
    } 
} 

Модуль клиента:

public class ClientMain { 

    public static void main(String[] argv) throws Exception { 

    Socket sock = new Socket("127.0.0.1", 6789); 

    byte[] mybytearray = new byte[1024]; 

    InputStream is = sock.getInputStream(); 

    FileOutputStream fos = new FileOutputStream("Demo1.java"); 

    BufferedOutputStream bos = new BufferedOutputStream(fos); 

    int count; 

      while ((count = is.read(mybytearray)) >= 0) { 

      System.out.println(count); 

         bos.write(mybytearray, 0, count); 
      } 

    bos.close(); 
    sock.close(); 
    } 
} 

Вот это работает для меня, и я заметил из кода, который вы взяли

byte[] mybytearray = new byte[(int) file.length()];

должно быть

byte[] mybytearray = new byte[1024];

надеюсь, что это будет работать для вас.

+0

вы можете отправлять файлы .jped или .png, не получая его поврежденным? –

+0

Да. Я попробовал его с .jpg файлом, и он дает результат, как ожидалось. –

+0

Спасибо, что был очень hepful –

1

Если len равно нулю, тогда никакие байты не считываются и возвращается 0; в противном случае возникает попытка прочитать хотя бы один байт. Если байт недоступен, потому что поток находится в конце файла, возвращается значение -1; в противном случае, по меньшей мере, один байт считывается и сохраняется в b.

Ваш файл достигнут Конец потока, поэтому его возврат -1. Таким образом, вы получаете ArrayIndexOutOfException

Заменить этот код

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 = 0; 
while((bytesRead = is.read(mybytearray, 0, mybytearray.length))>0) 
{  
     System.out.println("mybytearray : " +mybytearray.length +" bytesRead :"+bytesRead); 
     bos.write(mybytearray, 0, bytesRead); 
     System.out.println("8"); 
} 

    bos.flush(); 
System.out.println("9"); 
bos.close(); 
sock.close(); 
+0

YEs работает нормально в моей системе, но когда я запускаю его в сети, я получаю сообщение об ошибке. Я использовал IP = Inet4Address.getLocalHost(). GetHostAddress(); чтобы получить ip системы. Что теперь могло пойти не так? –

+1

Замените ваш сервер ip и серверным портом для прослушивания. Socket sock = новый Socket («Сервер IP», серверный порт); Убедитесь, что ваше сетевое соединение связано с ping сервером ip (введите ping serverip в командной строке Windows) с вашего клиентского компьютера. – Prabhakaran

+0

Как глупо от меня, спасибо за обновление. –

Смежные вопросы