Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.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将其直接存储到Mongo DB中_Java_Mongodb - Fatal编程技术网

从网页下载图像,并使用Java将其直接存储到Mongo DB中

从网页下载图像,并使用Java将其直接存储到Mongo DB中,java,mongodb,Java,Mongodb,从网页下载图像,而无需创建本地/temp图像文件。使用Java将图像直接存储到Mongo DB中 public void downloadImage() { String userAgent = "Mozilla/5.0 (jsoup)"; int timeout = 5 * 1000; String url = "<url>"; String fileName = "<fileName>"; Response respo

从网页下载图像,而无需创建本地/temp图像文件。使用Java将图像直接存储到Mongo DB中

public void downloadImage() {
     String userAgent = "Mozilla/5.0 (jsoup)";
     int timeout = 5 * 1000;
     String url = "<url>";
     String fileName = "<fileName>";
     Response response = Jsoup.connect(url).userAgent(userAgent).timeout(timeout).followRedirects(true).execute();

     DB db = mongoClient.getDB("<db_name>");
     DBCollection collection = db.getCollection("<db_collection>");

     ByteArrayOutputStream buffer = new ByteArrayOutputStream();

     byte[] buf = new byte[16384];

     InputStream img = getImage(url);

     if(img!=null) {
        for (int readNum; (readNum = img.read(buf)) != -1;) {
        //Writes to this byte array output stream
            buffer.write(buf, 0, readNum);
        }
        byte[] bytes = buffer.toByteArray();

        InputStream in = new ByteArrayInputStream(bytes);

        BufferedInputStream bis = new BufferedInputStream(in);

        GridFS gridfs = new GridFS(db, collection);
        GridFSInputFile gfsFile = gridfs.createFile(bis, fileName);
        fileID = gfsFile.getId();
        System.out.println(fileID);
        gfsFile.setFilename(fileName);
        gfsFile.save();
     }
}

private InputStream getImage(String src) throws IOException {
        int indexname = src.lastIndexOf("/");
        if (indexname == src.length())
            src = src.substring(1, indexname);
        //Open a URL Stream
        URL url = new URL(src);
        InputStream in = url.openStream();
        try {
            in = url.openStream();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        finally {
            in.close();
        }
        return in;
    }