2012-05-07 86 views
0

我想通过块读本地storgae的视频文件并上传到服务器。我有这个代码在另一个Java平台上工作,所以我认为它会很简单。为什么FileInputStream关闭

当我尝试使用

File f = new File(filePath); 
fileIn = new FileInputStream(f); 

它打开,我可以什么读什么我从文件的需要,在我的代码进一步下跌,虽然我打电话

SocketFactory socketFactory = SSLSocketFactory.getDefault(); 
    Socket socket = socketFactory.createSocket(url, 443); 

    _in = new InputStreamReader(socket.getInputStream()); 
    _out = (OutputStream)socket.getOutputStream(); 

的插座连接到打开文件好,但是当我在读取这段代码之后读取FileInputStream时,我得到流关闭异常。

任何想法?我没有看到日志中的任何内容显示任何失败,但是我无法从fileinputstream读取一旦我已经认识到服务器?

让我知道你是否需要知道任何其他的帮助。

+1

你还在读什么? – npinti

+0

这正是你的代码?是否有任何try/catch块? – nsfyn55

+1

请加上阅读代码.. – Ronnie

回答

-1

你没有调用socket.startHandshake();

+0

startHandshake似乎没有提供给Socket类,我正在阅读使用.. byte [] bit = new byte [bufSize - firstTotal.length]; int n = fileIn.read(bit); – user1379811

+0

-1。 ''startHandshake()'不可能解决任何意外关闭'FileInputStream'的问题,而且您根本不需要调用它:当您在SSLSocket上执行第一个I/O时,会自动调用它。 – EJP

1

这个例子为我工作:

public class MainClass { 
    public static void main(String[] args) { 
    String host = args[0]; 
    int port = Integer.parseInt(args[1]); 

    try { 
     System.out.println("Locating socket factory for SSL..."); 
     SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault(); 

     System.out.println("Creating secure socket to " + host + ":" + port); 
     SSLSocket socket = (SSLSocket) factory.createSocket(host, port); 

     System.out.println("Enabling all available cipher suites..."); 
     String[] suites = socket.getSupportedCipherSuites(); 
     socket.setEnabledCipherSuites(suites); 

     System.out.println("Registering a handshake listener..."); 
     socket.addHandshakeCompletedListener(new MyHandshakeListener()); 

     System.out.println("Starting handshaking..."); 
     socket.startHandshake(); 

     System.out.println("Just connected to " + socket.getRemoteSocketAddress()); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    } 
} 

class MyHandshakeListener implements HandshakeCompletedListener { 
    public void handshakeCompleted(HandshakeCompletedEvent e) { 
    System.out.println("Handshake succesful!"); 
    System.out.println("Using cipher suite: " + e.getCipherSuite()); 
    } 
} 



作为snicolas说,socket.startHandshake()可以解决您的概率。

+0

感谢你们这么快,让我看看信息在哪里让我和生病让你知道尽快。 – user1379811