本文介绍了关于使用socket连接传输文件到服务器的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以将文件传送到使用Java的服务器,但我不能使用socket编程的机器人来传输文件。我该怎么做呢?这里是我的Java code:

 进口java.io.BufferedReader中;
进口的java.io.File;
进口java.io.FileInputStream中;
进口java.io.FileNotFoundException;
进口java.io.FileOutputStream中;
进口java.io.FileReader;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.io.OutputStream中;
进口的java.net.Socket;
进口的java.net.UnknownHostException;

公共类客户{
    InputStream的是;
    OutputStream的操作系统;
    的FileReader FREAD;
    BufferedReader类bfrewad;
    的FileInputStream FIS;
    FileOutputStream中FOS;
    静态客户端的Socket;
    字符串str =;
    静态文件F1;

    公共静态无效的主要(字串[] args){
         F1 =新的文件(abc.dat);
        客户端C1 =新客户();
        尝试 {
            //c1.writeToAlocalfilefortesting();
            客户端=新的Socket(192.168.0.180,34000);
            的System.out.println(client.isBound());
            的System.out.println(套接字创建);
            c1.createAlinkForFile();
        }赶上(UnknownHostException异常E){
            // TODO自动生成的catch块
            的System.out.println(--------+ e.toString());
            e.printStackTrace();
        }赶上(IOException异常E){
            // TODO自动生成的catch块
            的System.out.println(--------+ e.toString());
            e.printStackTrace();
        }
        赶上(例外五){
            // TODO自动生成的catch块
            的System.out.println(--------+ e.toString()+-----);
            e.printStackTrace();
        }
    }

    私人无效writeToAlocalfilefortesting()抛出IOException异常{
        的System.out.println(内部函数);
         // TODO自动生成方法存根
        INT LEN;
        诠释计数= 0;
        的System.out.println(在这里);
        档案文件=新的文件(Nas123456789123.dat);
        的System.out.println(打开文件);
        FIS =新的FileInputStream(文件);
        的System.out.println(获取输入流);
        byte []的缓冲区=新的字节[25];
        OS =新的FileOutputStream(F1);
        的System.out.println(明放流);
        // INT I = fis.read(BUF)
        而((LEN = fis.read(缓冲液))大于0){
        如果(LEN == 25){
            数=计+ len个;
            fos.write(缓冲液,0,LEN);
            的System.out.println(sritten);
        }
        }
    }

    私人无效createAlinkForFile()抛出IOException异常{
        // TODO自动生成方法存根
        OS = client.getOutputStream();
        档案文件=新的文件(Nas123456789123.dat);
        FIS =新的FileInputStream(文件);
        byte []的B =新的字节[25];
        INT LEN1;
        INT的count1 = 0;
        而((LEN1 = fis.read(b))的大于0){
            如果(LEN1 == 25){
                COUNT1 = COUNT1 + LEN1;
                os.write(B,0,LEN1);
                os.flush();
                的System.out.println(sritten);
            }
       }
    }
}
 

解决方案

INT filelenghth =(INT)myFile.length();

字节[] mybytearray =新的字节[filelenghth];

的BufferedInputStream bis1 =新的BufferedInputStream(新的FileInputStream(MYFILE));

bis1.read(mybytearray,0,mybytearray.length);

dos.write(mybytearray,0,mybytearray.length); //数据的OutputStream

I'm able to transfer a file to the server using Java, but I'm not able to transfer the file using socket programming in Android. How do I do this? Here is my Java code:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class Client {
    InputStream is;
    OutputStream os;
    FileReader fread;
    BufferedReader bfrewad;
    FileInputStream fis;
    FileOutputStream fos;
    static Socket client;
    String str="";
    static File f1;

    public static void main(String[] args) {
         f1=new File("abc.dat");
        Client c1=new Client();
        try {
            //c1.writeToAlocalfilefortesting();
            client = new Socket("192.168.0.180", 34000);
            System.out.println(client.isBound());
            System.out.println("socket created ");
            c1.createAlinkForFile();    
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            System.out.println("--------"+e.toString());
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("--------"+e.toString());
            e.printStackTrace();
        }
        catch (Exception e) {
            // TODO Auto-generated catch block
            System.out.println("--------"+e.toString()+"-----");
            e.printStackTrace();    
        }
    }

    private void writeToAlocalfilefortesting() throws IOException {
        System.out.println("inside the function");
         //TODO Auto-generated method stub
        int len;
        int count=0;
        System.out.println("here ");
        File file=new File("Nas123456789123.dat");
        System.out.println("file opened");
        fis=new FileInputStream(file);
        System.out.println("getting input stream");
        byte[] buffer =new byte[25];
        os=new FileOutputStream(f1);
        System.out.println("out put stream ");
        //int i=fis.read(buf)
        while((len=fis.read(buffer))>0){
        if(len==25){
            count=count+len;
            fos.write(buffer, 0, len);
            System.out.println("sritten");
        }
        }
    }

    private  void createAlinkForFile() throws IOException {
        // TODO Auto-generated method stub
        os=client.getOutputStream();    
        File file=new File("Nas123456789123.dat");
        fis=new FileInputStream(file);
        byte[] b =new byte[25]; 
        int len1;
        int count1=0;
        while((len1=fis.read(b))>0){
            if(len1==25){
                count1=count1+len1;
                os.write(b, 0, len1);
                os.flush();
                System.out.println("sritten");
            }
       }
    }
}
解决方案

int filelenghth=(int)myFile.length();

byte [] mybytearray = new byte [filelenghth];

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

bis1.read(mybytearray,0,mybytearray.length);

dos.write(mybytearray,0,mybytearray.length); // data outputstream

这篇关于关于使用socket连接传输文件到服务器的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 12:25