服务器返回非法容量-1

用户名

在这里,我将发布一个完美的示例,说明如何通过套接字发送和接收图像,音乐,视频或其他任何内容,以便任何需要的人都可以使用它。它几乎是完美的。它可以完成工作,无论目标中有多少文件,它都会对它们进行计数并将其发送到Java服务器,但是至少在服务器端我收到了异常:

 Exception in thread "main" java.lang.IllegalArgumentException: Illegal Capacity: -1
at java.util.ArrayList.<init>(Unknown Source)

我真的不知道那是什么,但是如果您能帮助我,我将不胜感激,所以我为自己和任何其他需要自己的人补充了这一点。谢谢

Android Cliend:

 package com.example.imagesender;

//File imagefile = new File(filepath);
//  FileInputStream fis = null;
//       try {
//           fis = new FileInputStream(imagefile);
//      } catch (FileNotFoundException e) {
 //          System.out.println("file not found");
 //        e.printStackTrace();
 //    }

 // Bitmap bm = BitmapFactory.decodeStream(fis);
//  imgbyte = new byte [(int)filepath.length()];
//   imv.setImageBitmap(bm);
//  Log.d("ClientActivity","length:"+imgbyte);




public class AccountCreator extends Activity {  
    private Button send;  

    private Socket socket;  
    private File f,fdst;  
    private FileInputStream fin,fises;  
    private static Context context;  

    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.mes_registerpage);  
        send=(Button)findViewById(R.id.sendpic);  
      //  sendWan=(Button)findViewById(R.id.sendWan);  
        AccountCreator.context = getApplicationContext();  

        send.setOnClickListener(new OnClickListener(){  

            public void onClick(View view){  

                this.doit();
            }
                class myAsync extends AsyncTask<String, Boolean, Boolean>{


                                 @Override
                    protected Boolean doInBackground(String...urls){    
                try {  
                    socket = new Socket("10.0.2.2", 1500);  
                    System.out.println(socket);  
                    System.out.println("Connecting...");  


                    File fil=new 
 File(Environment.getExternalStorageDirectory()+"/Pictures/");  
                      System.out.println(fil);
                    File[] Files=fil.listFiles();  
                    System.out.println(Files);
                    for (int count=0;count < Files.length;count ++){  
                        System.out.println(Files[count].getName());  

                    }  

                    OutputStream os = socket.getOutputStream();    
                    DataOutputStream dos = new DataOutputStream(os);   

                    dos.writeInt(Files.length);  

                    for (int count=0;count<Files.length;count ++){  
                          dos.writeUTF(Files[count].getName());  

                    }  
                    for (int count=0;count<Files.length;count ++){  

                          int filesize = (int) Files[count].length();  
                          dos.writeInt(filesize);  
                    }  

                    for (int count=0;count<Files.length;count ++){  

                    int filesize = (int) Files[count].length();  
                    byte [] buffer = new byte [filesize];  

                    FileInputStream fis = new  
 FileInputStream(Files[count].toString());    
                    BufferedInputStream bis = new BufferedInputStream(fis);    

                    //Sending file name and file size to the server    
                    bis.read(buffer, 0, buffer.length); //This line is important  
                    dos.write(buffer, 0, buffer.length);     
                    dos.flush();   

                    //close socket connection  
                    //socket.close();  

                }  

                           //  Toast.makeText(getApplicationContext(),"Transfer file 
is completed!!", Toast.LENGTH_LONG).show();   
                     socket.close();  
                }  
                catch(Exception e){  
                    System.out.println("Error::"+e);  
                    //System.out.println(e.getMessage());  
                    //e.printStackTrace();  
                    //Log.i("******* :( ", "UnknownHostException");  
                }
                return null;

            }  
                }



                protected void onPostExecute(String result){
                }


public void doit(){
myAsync sync = new myAsync();
sync.execute();
}
        });}

    }  

和服务器端:

 public class Server {  

    /** 
     * @param args 
     */  
    public static void main(String[] args) throws IOException,EOFException {  
        // TODO Auto-generated method stub  
     FileOutputStream fos;  
     BufferedOutputStream bos;  
     OutputStream output;  
     DataOutputStream dos;  
     int len;  
     int smblen;   
     InputStream in;  
     DataInputStream clientData;  
     BufferedInputStream clientBuff;  


    ServerSocket serverSocket = new ServerSocket(1500);  
    Socket clientSocket = null;  
    clientSocket = serverSocket.accept();  


        in = clientSocket.getInputStream(); //used    

       clientData = new DataInputStream(in); //use    
       clientBuff = new BufferedInputStream(in); //use    
      int N=1;
         while(N==1){    

            System.out.println("Starting...");    

               int fileSize = clientData.read();        
                        fileSize = (fileSize > 0) ? fileSize:0;      


                        List<File> files = new ArrayList<>(fileSize);          
                        List<Integer> sizes = new ArrayList<>(fileSize);

                for (int count=0;count < fileSize;count ++){  

                        sizes.add(clientData.readInt());  
                }  

               for (int count =0;count < fileSize ;count ++){     

                  len=sizes.get(count);  

                System.out.println("File Size ="+len);  

                //output = new FileOutputStream("C:/share/" + fileName);  
                output = new FileOutputStream("D://Users/" + files.get(count));  
                dos=new DataOutputStream(output);  
                bos=new BufferedOutputStream(output);  

                byte[] buffer = new byte[1024];    

                bos.write(buffer, 0, buffer.length); //This line is important  

                while (len > 0 && (smblen = clientData.read(buffer)) > 0) {   
                    dos.write(buffer, 0, smblen);   
                      len = len - smblen;  
                      dos.flush();  
                    }    
            N=2;
              }   
       }  

      } //end loop   

 }  

提前致谢

stacktrace:

 Exception in thread "main" java.lang.IllegalArgumentException: Illegal Capacity: -1
at java.util.ArrayList.<init>(Unknown Source)
at Server.main(Server.java:49)
埃利奥特·弗里斯(Elliott Fresh)

不要将负值传递给ArrayList构造函数。您也可以使用菱形运算符(在Java 7+中),并且您可能更喜欢使用该List接口。所以,像这样-

int fileSize = clientData.read();
fileSize = (fileSize > 0) ? fileSize : 0;             // guard against negatives.
List<File> files = new ArrayList<>(fileSize);         // Using List and <>
List<Integer> sizes = new ArrayList<>(fileSize);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

从android中的服务器返回文件大小= -1

ArrayIndexOutOfBoundsException:Web服务器中的1

MySQL 查询在 azure 应用服务 + azure 数据库服务器中始终只返回“1”

设计数据结构以返回最近1分钟与Web服务器的连接数

在JavaScript中从服务器1到服务器2执行Ajax

Django开发服务器的容量

超时 1 小时的无服务器 python?

在Angular 1中从API服务器提取JSON数据

1个IP但多个Web服务器的子域

可以禁用sbt 1.x服务器吗?

在服务器响应中提升Beast 1秒延迟

无法通过 jenkins 向 gerrit 服务器报告 +1

2个域1个服务器

1 个域上的 2 个 teampeak 服务器?

Ubuntu 服务器硬件 RAID 1 扩展

SSL_connect返回= 1 errno = 0状态= SSLv3读取服务器证书B:仅在代理时证书验证失败

Google Oauth SSL错误-SSL_connect返回= 1 errno = 0状态= SSLv3读取服务器证书B:证书验证失败

使用openshift nodejs应用接收“ SSL_connect返回= 1 errno = 0状态= SSLv3读取服务器问候A:sslv3警报握手失败”

Heroku Rails Net :: HTTP:OpenSSL :: SSL :: SSLError:SSL_connect返回= 1 errno = 0状态= SSLv3读取服务器证书B:证书验证失败

启动 wso2 服务器时出错:org.wso2.carbon.user.core.UserStoreException:错误!子查询返回超过 1 行

SSL_connect返回= 1 errno = 0状态= SSLv3读取服务器证书B:证书验证失败的MAC

试图在telnet中运行原始ftp命令时,vsftp服务器返回500个非法PORT命令

Shell 脚本:如何将文件列表从服务器 1 复制到服务器 2

当服务器2完成特定工作时,服务器1如何可靠地知道?(bull.js)

从服务器 1 创建邀请并发送给服务器 2 中的用户 DMS

在第1行的'itid = 1'附近使用正确语法的MySQL服务器版本

使用1and1云服务器发送邮件失败

Symfony SQLSTATE [HY000] [2002]与1and1服务器的连接被拒绝

无法下载Composer-具有SSH访问服务器的1and1