简体   繁体   English

getResourceAsStream返回的不是整个文件的HttpInputStream

[英]getResourceAsStream returns HttpInputStream not of the entire file

I am having a web application with an applet which will copy a file packed witht the applet to the client machine. 我有一个带有applet的Web应用程序,它将把与applet一起打包的文件复制到客户端计算机上。

When I deploy it to webserver and use: InputStream in = getClass().getResourceAsStream("filename") ; 当我将其部署到Web服务器并使用时:InputStream in = getClass()。getResourceAsStream(“ filename”);

The in.available() always return a size of 8192 bytes for every file I tried, which means the file is corrupted when it is copied to the client computer. 对于我尝试的每个文件,in.available()始终返回8192字节的大小,这意味着该文件在复制到客户端计算机时已损坏。

The InputStream is of type HttpInputStream (sun.net.protocol.http.HttpUrlConnection$httpInputStream). InputStream的类型为HttpInputStream(sun.net.protocol.http.HttpUrlConnection $ httpInputStream)。 But while I test applet in applet viewer, the files are copied fine, with the InputStream returned is of type BufferedInputStream, which has the file's byte sizes. 但是,当我在applet Viewer中测试applet时,文件被很好地复制,返回的InputStream类型为BufferedInputStream,具有文件的字节大小。 I guess that when getResourceStream in file system the BufferedInputStream will be used and when at http protocol, HttpInputStream will be used. 我猜想,当在文件系统中使用getResourceStream时,将使用BufferedInputStream;而在使用HTTP协议时,将使用HttpInputStream。

How will I copy the file completely, is there a size limited for HttpInputStream? 如何完全复制文件,HttpInputStream是否有大小限制? Thanks a lot. 非常感谢。

in.available() tells you how many bytes you can read without blocking, not the total number of bytes you can read from a stream. in.available()告诉您可以无阻塞地读取多少个字节,而不是可以从流中读取的字节总数。

Here's an example of copying an InputStream to an OutputStream from org.apache.commons.io.IOUtils : 这是从org.apache.commons.io.IOUtilsInputStream复制到OutputStream的示例:

public static long copyLarge(InputStream input, OutputStream output)
        throws IOException {
    byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
    long count = 0;
    int n = 0;
    while (-1 != (n = input.read(buffer))) {
        output.write(buffer, 0, n);
        count += n;
    }
    return count;
}

The in.available() always return a size of 8192 bytes for every file I tried, which means the file is corrupted when it is copied to the client computer. 对于我尝试的每个文件,in.available()始终返回8192字节的大小,这意味着该文件在复制到客户端计算机时已损坏。

It does not mean that at all! 这一点都不意味着!

The in.available() method returns the number of characters that can be read without blocking. in.available()方法返回不阻塞即可读取的字符数。 It is not the length of the stream. 它不是流的长度。 In general, there is no way to determine the length of an InputStream apart from reading (or skipping) all bytes in the stream. 通常,除了读取(或跳过)流中的所有字节外,无法确定InputStream的长度。

(You may have observed that new FileInputStream("someFile").available() usually gives you the file size. But that behaviour is not guaranteed by the spec, and is certainly untrue for some kinds of file, and possibly for some kinds of file system as well. A better way to get the size of a file is new File("someFile").length() , but even that doesn't work in some cases.) (您可能已经观察到, new FileInputStream("someFile").available()通常会为您提供文件大小。但是该行为不受规范保证,对于某些类型的文件(可能对于某些类型的文件)肯定是不正确的)文件系统。获取文件大小的更好方法是new File("someFile").length() ,但在某些情况下甚至无效。)

See @tdavies answer for example code for copying an entire stream's contents. 有关复制整个流内容的示例代码,请参见@tdavies答案。 There are also third party libraries that can do this kind of thing; 也有第三方图书馆可以做这种事情。 eg org.apache.commons.net.io.Util . 例如org.apache.commons.net.io.Util

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM