Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Java中的PHP readfile()下载zip文件_Java_Php_Download_Zip - Fatal编程技术网

从Java中的PHP readfile()下载zip文件

从Java中的PHP readfile()下载zip文件,java,php,download,zip,Java,Php,Download,Zip,我想通过HTTP和PHP下载一个使用Java的zip文件。我需要这个,因为我需要PHP在让用户下载之前进行一些授权 授权成功后,页面使用以下代码发送文件: if(file_exists($filename)){ //Use Content-Disposition: attachment to specify the filename header("Content-type: application/zip"); header('Content-Disposition:

我想通过HTTP和PHP下载一个使用Java的zip文件。我需要这个,因为我需要PHP在让用户下载之前进行一些授权

授权成功后,页面使用以下代码发送文件:

if(file_exists($filename)){

    //Use Content-Disposition: attachment to specify the filename
    header("Content-type: application/zip");
    header('Content-Disposition: attachment; filename='.basename($filename));

    //No cache
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');

    //Define file size
    header('Content-Length: ' . filesize($filename));
    ob_clean();
    flush();
    echo "DOWNLOAD_START\r\n";
    readfile($filename);
    exit;
}else{
    clearstatcache();
    echo "No download available.";
}
通过浏览器下载时,下载效果良好。我通过简化尝试提取来验证它是否工作正常。Java应用程序生成的版本已损坏

因为页面也输出标题信息,所以我回显“DOWNLOAD\u START\r\n”以表示zip文件何时启动。下载ZIP文件的代码为:

InputStream sockin = keygenSock.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(sockin));
System.out.println("Reading...");
for(String line = reader.readLine(); line != null; line = reader.readLine()){
    System.out.println(line);
    if(line.startsWith("DOWNLOAD_START")){
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("contents.zip")));
        int read;
        int size = 0;
        byte[] buffer = new byte[2048];
        while((read = sockin.read(buffer, 0, 2048)) >= 0){
            size += read;
            bos.write(buffer, 0, read);
        }
        System.out.println("SIZE READ: " + size);
        bos.flush();
        bos.close();
        //zip(sockin);
        break;
    }
}
读取的实际字节数不等于以字节为单位的实际文件大小。所以我猜我下载ZIP文件的方式有错误

如何让Java客户端在授权后从页面下载zip文件

同样,错误是下载的ZIP文件比实际的ZIP文件小

谢谢


编辑:如果我只是将输出的全部转储到zip文件中,我就可以使用winRAR提取它。但是,如果我在文本编辑器中打开它,我可以看到所有的标题信息和实际zip文件之前的“DOWNLOAD_START”。这会导致Java解压类(ZipInputStream和ZipEntry)失败,因此即使使用winRAR之类的软件正确提取,我也不需要包含这些类。

问题在于BufferedReader,它将读取/消耗流中的多行数据

所以要么使用无缓冲的阅读器。这意味着您没有readline方法

StringBuilder line = new StringBuilder();
int character;
while ((character = reader.read()) >= 0 && character != '\n') {
    line.append(character);
}
或者只使用对InputStream执行相同的操作

StringBuilder line = new StringBuilder();
int character;
while ((character = stream.read()) >= 0 && character != '\n') {
    line.append(character);
}

如果存在,两行都将包含\r(因此相应地更改代码)。只要所使用的编码不会弄乱较低的127字节,无论哪种方式都可以。

谢谢!我不知道BufferedReader,我可以处理无缓冲流。