使用套接字连接将文件传输到服务器的问题

时间:2010-12-13 21:22:50

标签: android

我可以使用Java将文件传输到服务器,但是我无法使用Android中的套接字编程传输文件。我该怎么做呢?这是我的Java代码:

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

1 个答案:

答案 0 :(得分:1)

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); //数据输出流