Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.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 如果服务器没有';不响应文件下载_Java - Fatal编程技术网

Java 如果服务器没有';不响应文件下载

Java 如果服务器没有';不响应文件下载,java,Java,我使用下面的代码从Kaltura服务器下载文件。如果我们从服务器上得到错误,我们可以识别吗。 所以,对于我的情况,我没有从服务器得到错误,空白文件正在下载,但我不想下载该文件。相反,我想返回错误消息 String s = "https://www.kaltura.com/p/588888/sp.....; try { URL url = new URL(s); InputStream in = new Buffe

我使用下面的代码从Kaltura服务器下载文件。如果我们从服务器上得到错误,我们可以识别吗。 所以,对于我的情况,我没有从服务器得到错误,空白文件正在下载,但我不想下载该文件。相反,我想返回错误消息

        String s = "https://www.kaltura.com/p/588888/sp.....;   
        try {
            URL url = new URL(s);
           InputStream in = new BufferedInputStream(url.openStream());
            res.setContentType("application/octet-stream");
            res.setHeader("Cache-Control", "no-cache");
            res.setHeader("Content-Disposition","attachment;filename="+fileName);
            res.setStatus(200);
            OutputStream out = res.getOutputStream();
            byte[] buf = new byte[8192*2];
            while (in.read(buf) != -1)
            {
                out.write(buf);
            }
            out.flush();
            in.close();     
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

我已经使用ImageIO来读取这个示例中的图像。最简单的方法是将服务器上显示的空白图像保存到您的计算机上,我们将其命名为“blankImage.png”。当您从url中读取图像时,您可以比较新图像是否与计算机中的此图像相同。您不能简单地通过相等进行比较,因此需要另一种方法:BufferedImagesReequal()。然后你知道这是一张空白图像:

BufferedImage blankImage = ImageIO.read(new File("C:/path/blankImage.png"));
        URL url = new URL("https://targetImageUrl");
        BufferedImage newImage = ImageIO.read(url);

        if (bufferedImagesAreEqual(blankImage, newImage)) {
            throw new Exception("Image is blank!");
        } else {
            //return your image;
        }

private static boolean bufferedImagesAreEqual(BufferedImage img1, BufferedImage img2) {
        if (img1.getWidth() == img2.getWidth() && img1.getHeight() == img2.getHeight()) {
            for (int x = 0; x < img1.getWidth(); x++) {
                for (int y = 0; y < img1.getHeight(); y++) {
                    if (img1.getRGB(x, y) != img2.getRGB(x, y))
                        return false;
                }
            }
        } else {
            return false;
        }
        return true;
    }
BufferedImage blankImage=ImageIO.read(新文件(“C:/path/blankImage.png”);
URL=新URL(“https://targetImageUrl");
BuffereImage newImage=ImageIO.read(url);
if(bufferedImagesReequal(blankImage,newImage)){
抛出新异常(“图像为空!”);
}否则{
//返回您的图像;
}
专用静态布尔BuffereImage相等(BuffereImage img1、BuffereImage img2){
if(img1.getWidth()==img2.getWidth()&&img1.getHeight()==img2.getHeight()){
对于(int x=0;x
试试看


if(in.available()我试过使用下面的代码,效果很好

URL url = null;
    try {
        url = new URL(kalturaUrl);
        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
        if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
            InputStream inputStream = httpConn.getInputStream();
            response.setContentType(httpConn.getContentType());
            response.setContentLength(httpConn.getContentLength());
            response.setHeader("Content-Disposition", httpConn.getHeaderField("Content-Disposition"));
            OutputStream outputStream = response.getOutputStream();

            int bytesRead = -1;
            byte[] buffer = new byte[8192];
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }

            outputStream.close();
            inputStream.close();
        } else {
            return "No file to download. Server replied HTTP code: " + httpConn.getResponseCode();
        }
        httpConn.disconnect();
    } catch (MalformedURLException e) {
        logger.error("Exception : ",e);
    } catch (IOException e) {
        logger.error("Exception : ",e);
    }

不太清楚你在问什么,但为什么不记下字节数
读取
-如果
为零
,那么你可以自己做逻辑除了你的目标之外,你的写循环也是错误的:你必须跟踪
in.read()
读取的字节数,只在
out.write()中写入那么多字节
buf
可能没有完全填满每个读取请求!它可能返回204(无内容),您可以调试并查看
URL url = null;
    try {
        url = new URL(kalturaUrl);
        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
        if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
            InputStream inputStream = httpConn.getInputStream();
            response.setContentType(httpConn.getContentType());
            response.setContentLength(httpConn.getContentLength());
            response.setHeader("Content-Disposition", httpConn.getHeaderField("Content-Disposition"));
            OutputStream outputStream = response.getOutputStream();

            int bytesRead = -1;
            byte[] buffer = new byte[8192];
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }

            outputStream.close();
            inputStream.close();
        } else {
            return "No file to download. Server replied HTTP code: " + httpConn.getResponseCode();
        }
        httpConn.disconnect();
    } catch (MalformedURLException e) {
        logger.error("Exception : ",e);
    } catch (IOException e) {
        logger.error("Exception : ",e);
    }