本文整理匯總了Java中com.mongodb.gridfs.GridFSDBFile.writeTo方法的典型用法代碼示例。如果您正苦於以下問題:Java GridFSDBFile.writeTo方法的具體用法?Java GridFSDBFile.writeTo怎麽用?Java GridFSDBFile.writeTo使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.mongodb.gridfs.GridFSDBFile
的用法示例。
在下文中一共展示了GridFSDBFile.writeTo方法的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: execute
import com.mongodb.gridfs.GridFSDBFile; //導入方法依賴的package包/類
public String execute() throws IOException {
String[] fids = fid.split(",");
File[] files = new File[fids.length];
String username = (String) httpSession.get("username");
System.out.println("fids: " + Arrays.toString(fids));
for (int i = 0; i < files.length; i++) {
GridFSDBFile gridFSDBFile = boxService.download(fids[i]);
files[i] = new File(gridFSDBFile.getFilename());
gridFSDBFile.writeTo(files[i]);
}
filename = "/tmp/download.zip";
ZipFileUtil.compressFiles2Zip(files, filename);
inputStream = new FileInputStream(new File(filename));
filename = "download.zip";
return SUCCESS;
}
示例2: get
import com.mongodb.gridfs.GridFSDBFile; //導入方法依賴的package包/類
@RequestMapping({"/{filename:.+}"})
public void get(
@PathVariable final String filename,
final HttpServletRequest request,
final HttpServletResponse response
) throws IOException {
final GridFSDBFile file = this.gridFs.findOne(Query.query(Criteria.where("filename").is(filename)));
if (file == null) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
} else {
final int cacheForDays = 365;
response.setHeader("Content-Type", file.getContentType());
response.setHeader("Content-Disposition", String.format("inline; filename=\"%s\"", file.getFilename()));
response.setHeader("Expires", now(of("UTC")).plusDays(cacheForDays).format(RFC_1123_DATE_TIME));
response.setHeader("Cache-Control", String.format("max-age=%d, %s", TimeUnit.DAYS.toSeconds(cacheForDays), "public"));
file.writeTo(response.getOutputStream());
response.flushBuffer();
}
}
示例3: execute
import com.mongodb.gridfs.GridFSDBFile; //導入方法依賴的package包/類
@Override
public void execute() throws Exception {
MongoClient mdb = MongoFactory.getInst().getMongo( sName );
if ( mdb == null )
throw new Exception("no server selected");
if ( sDb == null )
throw new Exception("no database selected");
MongoFactory.getInst().setActiveDB(sDb);
DB db = mdb.getDB(sDb);
GridFS gfs = new GridFS( db, sColl.substring(0,sColl.lastIndexOf(".")) );
GridFSDBFile gridFSDBFile = gfs.find(id);
gridFSDBFile.writeTo(saveFile);
setMessage( "fileSaved=" + saveFile + "; size=" + saveFile.length() );
}
示例4: prefetchData
import com.mongodb.gridfs.GridFSDBFile; //導入方法依賴的package包/類
public DataAddress prefetchData(DataAddress givenAddress, ServerAddress destAddress) throws IOException {
logger.info("yo2");
ServerAddress givenServer = new ServerAddress(givenAddress.hostname, givenAddress.port);
GridFS givenDatabase = connectToDatabase(givenServer);
logger.info("yo");
GridFSDBFile givenPackage = givenDatabase.findOne(new BasicDBObject("_id", givenAddress.ID));
ByteArrayOutputStream baos = new ByteArrayOutputStream((int)givenPackage.getLength());
givenPackage.writeTo(baos);
logger.info("Prefetched");
GridFS destDatabase = connectToDatabase(destAddress);
GridFSInputFile destPackage = destDatabase.createFile(baos.toByteArray());
int newID = getNextId(destDatabase);
logger.info("Got new id for prefetched package: " + newID);
destPackage.put("_id", newID);
destPackage.save();
logger.info("after save");
DataAddress ret = new DataAddress();
ret.hostname = destAddress.getHost();
ret.port = destAddress.getPort();
ret.ID = newID;
return ret;
}
示例5: retriveDataBase
import com.mongodb.gridfs.GridFSDBFile; //導入方法依賴的package包/類
public OutputStream retriveDataBase(String filename) throws IOException {
// return file stream
ByteArrayOutputStream os = new ByteArrayOutputStream();
// get the file
GridFSDBFile out = model.findOne(filename);
// save to the file
out.writeTo(os);
// return
return os;
}
示例6: retriveDataBase
import com.mongodb.gridfs.GridFSDBFile; //導入方法依賴的package包/類
public OutputStream retriveDataBase(String filename) throws IOException{
// return file stream
ByteArrayOutputStream os = new ByteArrayOutputStream();
// get the file
GridFSDBFile out = model.findOne(filename);
// save to the file
out.writeTo(os);
// return
return os;
}
示例7: gridFSDBFile2ByteArray
import com.mongodb.gridfs.GridFSDBFile; //導入方法依賴的package包/類
public static byte[] gridFSDBFile2ByteArray(GridFSDBFile gridFSDBFile) {
byte[] bytes;
try (ByteArrayOutputStream byteArrayOutputStream1 = new ByteArrayOutputStream()) {
gridFSDBFile.writeTo(byteArrayOutputStream1);
bytes = byteArrayOutputStream1.toByteArray();
} catch (IOException | NullPointerException e) {
LOGGER.error(e.getMessage());
return null;
}
return bytes;
}
示例8: serveFile
import com.mongodb.gridfs.GridFSDBFile; //導入方法依賴的package包/類
private void serveFile(String path,
HttpServletRequest request,
HttpServletResponse response) throws IOException{
GridFSDBFile file = HDFSArchiver.getArchiver().getFile(path);
if(file != null){
String content = null;
if(request.getParameter("download") == null){
content = file.getContentType();
}else {
content = "application/octet-stream";
}
if(content == null){
content = getContentType(path);
}
response.setContentType(content);
log.debug("Response file:" + path + ", content:" + content);
if(file.containsField("localPath")){
log.debug("local from local path:" + file.get("localPath"));
byte[] buffer = new byte[1024 * 40];
InputStream in = new FileInputStream(new File(file.get("localPath").toString()));
for(int len = buffer.length; len == buffer.length; ){
len = in.read(buffer);
response.getOutputStream().write(buffer, 0, len);
}
in.close();
}else {
file.writeTo(response.getOutputStream());
}
}else {
log.debug("Not found file:" + path);
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
response.getWriter().write("Not found file:" + path);
}
}
示例9: readContent
import com.mongodb.gridfs.GridFSDBFile; //導入方法依賴的package包/類
@Override
public void readContent(RequestContext ctx, BinaryContentSink sink) throws Exception {
GridFS gridfs = getUserspace().getGridFS();
// TODO - maybe use netty directly here?
// TODO - BinaryContentSink should flush to socket (pipeline channel), not queue to memory
// get tmp file
File tmpFile = getTempFile();
// sync copy from db to tmp file first - no async API
GridFSDBFile file = gridfs.findOne(new BasicDBObject("_id", fileInfo().getId()));
file.writeTo(tmpFile);
// async copy from local tmp file to sink
getRoot().vertx().fileSystem().open(tmpFile.getPath(), (result) -> {
if (result.succeeded()) {
AsyncFile asyncFile = result.result();
asyncFile.dataHandler((buffer) -> {
sink.accept(buffer.getByteBuf());
});
asyncFile.endHandler((end) -> {
sink.close();
asyncFile.close();
tmpFile.delete();
});
} else {
sink.close();
tmpFile.delete();
}
});
}
示例10: processScreenshots
import com.mongodb.gridfs.GridFSDBFile; //導入方法依賴的package包/類
@SuppressWarnings("serial")
public static ProcessedScreenshots processScreenshots(String baseScreenshotId, String newScreenshotId)
throws IOException {
String idOfTheDiffImage = "";
String diffImageFileName = String.format("%s|%s|%s", baseScreenshotId, newScreenshotId, "-differences");
boolean screenshotsHaveBeenReviewed = false;
BasicDBObject diffImageQuery = new BasicDBObject();
diffImageQuery.put("filename", diffImageFileName);
boolean diffImageExists = GFS_DIFF_PHOTOS.find(diffImageQuery).iterator().hasNext();
// in case the image has been already been generated provide the image, and not generate a new one
if (diffImageExists) {
GridFSDBFile diffFile = GFS_DIFF_PHOTOS.find(diffImageQuery).iterator().next();
idOfTheDiffImage = diffFile.getId().toString();
screenshotsHaveBeenReviewed =
Boolean.parseBoolean(diffFile.get("screenshotsHaveBeenReviewed").toString());
} else {
// generate the img with the differences from the base and tmp
BasicDBObject baseQuery = new BasicDBObject();
baseQuery.put("_id", new ObjectId(baseScreenshotId));
BasicDBObject tmpQuery = new BasicDBObject();
tmpQuery.put("_id", new ObjectId(newScreenshotId));
BasicDBObject tmpIgnoredZones = new BasicDBObject();
tmpIgnoredZones.put("imageId", new ObjectId(newScreenshotId));
Iterator<DBObject> its = DBConnectors.TMP_IMAGES.find(tmpIgnoredZones).iterator();
its.hasNext();
DBObject o = its.next();
Type type = new TypeToken<List<Rectangle>>() {
}.getType();
List<Rectangle> ignoreZones = GsonUtil.gson.fromJson(o.get("ignoreZones").toString(), type);
// pull images linked to the provided IDs from DB
GridFSDBFile gfsBaseFile = GFS_PHOTO.find(baseQuery).iterator().next();
GridFSDBFile gfsTmpFile = GFS_PHOTO.find(tmpQuery).iterator().next();
// if the MD5 are a match, it means the images are the same
if (gfsBaseFile.getMD5().equals(gfsTmpFile.getMD5())) {
return new ProcessedScreenshots(true, "200");
}
File baseFile = new File(baseScreenshotId);
File tmpFile = new File(newScreenshotId);
gfsBaseFile.writeTo(baseFile);
gfsTmpFile.writeTo(tmpFile);
ScreenshotProcessingResponse processResponse =
ScreenshotProcessing.processScreenshots(baseFile, tmpFile, ignoreZones);
// persist the diff file in the DB
GridFSInputFile fsDiffFile = GFS_DIFF_PHOTOS.createFile(processResponse.getFile());
fsDiffFile.setFilename(diffImageFileName);
fsDiffFile.put("screenshotsHaveBeenReviewed", false);
fsDiffFile.save();
idOfTheDiffImage = fsDiffFile.getId().toString();
baseFile.delete();
tmpFile.delete();
}
return new ProcessedScreenshots(screenshotsHaveBeenReviewed, idOfTheDiffImage);
}
示例11: doCoolMagicInside
import com.mongodb.gridfs.GridFSDBFile; //導入方法依賴的package包/類
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response doCoolMagicInside(String jsonReq, @Context Request request) throws IOException {
EndpointUtil.printClientInfo(request);
boolean baseImageFound = false;
CompareScreenshotRequest itemToCompare = GsonUtil.gson.fromJson(jsonReq, CompareScreenshotRequest.class);
DBObject base = null;
ObjectId baseImageObjectId = null;
GridFSDBFile baseScreenshot = null;
File baseFile = null;
File tmpFile = new File("tmp");
String reviewLink = "";
ScreenshotProcessingResponse processedResponse = null;
CompareScreenshotsResponse compareResponse = null;
// assuming this image has been uploaded, create a search for it in the tmp & base images table
BasicDBObject baseQuery = new BasicDBObject();
baseQuery.put(TEST_NAME, itemToCompare.getTestName());
baseQuery.put(TEST_BROWSER, itemToCompare.getTestBrowser());
baseQuery.put(DESCRIPTION, itemToCompare.getDescription());
BasicDBObject newScreenshotQuery = new BasicDBObject();
newScreenshotQuery.put(_ID, itemToCompare.getImageId());
BasicDBObject baseScreenshotQuery = new BasicDBObject();
// pull the base image object
DBCursor baseCursor = BASE_IMAGES.find(baseQuery);
if (baseCursor.hasNext()) {
baseImageFound = true;
base = baseCursor.next();
baseImageObjectId = new ObjectId(base.get("imageId").toString());
baseScreenshotQuery.put(_ID, baseImageObjectId);
baseScreenshot = GFS_PHOTO.find(baseScreenshotQuery).iterator().next();
baseFile = new File("base");
baseScreenshot.writeTo(baseFile);
} else {
// there is no base image stored for the baseQuery
}
GridFSDBFile newScreenshot = GFS_PHOTO.find(newScreenshotQuery).iterator().next();
newScreenshot.writeTo(tmpFile);
if (baseImageFound) {
reviewLink =
Main.getBaseUri() + "review/" + baseScreenshot.getId().toString() + "/"
+ newScreenshot.getId().toString();
processedResponse = ScreenshotProcessing.processScreenshots(baseFile, tmpFile, itemToCompare.getIgnoreZones());
compareResponse =
new CompareScreenshotsResponse(processedResponse.getStatus(), reviewLink,
processedResponse.getDiffPercentage());
} else {
reviewLink =
Main.getBaseUri() + "review-single/" + newScreenshot.getId().toString();
compareResponse = new CompareScreenshotsResponse(false, reviewLink, -1);
}
return Response.ok().entity(GsonUtil.gson.toJson(compareResponse, CompareScreenshotsResponse.class)).build();
}
示例12: writePackageToStream
import com.mongodb.gridfs.GridFSDBFile; //導入方法依賴的package包/類
public void writePackageToStream(long pageNo, boolean gzipped, OutputStream out) throws IOException {
if (pageNo < 1 || pageNo >= journal.getCurrentPageNo()) {
throw new IllegalArgumentException("Not a complete page: " + pageNo);
}
GridFSDBFile f = packageGridFs.findOne(pageNo + "");
OutputStream packageOut = null;
InputStream packageAsStream = null;
try {
if (f == null) {
if (gzipped) {
out = new GZIPOutputStream(out);
}
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
packageOut = new GZIPOutputStream(bOut);
String pageContent = journal.getPageContent(pageNo);
for (String uri : pageContent.split("\\n")) {
Nanopub np = getNanopub(TrustyUriUtils.getArtifactCode(uri));
String s;
try {
s = NanopubUtils.writeToString(np, RDFFormat.TRIG);
} catch (RDFHandlerException ex) {
throw new RuntimeException("Unexpected RDF handler exception", ex);
}
byte[] bytes = (s + "\n").getBytes();
out.write(bytes);
packageOut.write(bytes);
}
packageOut.close();
packageAsStream = new ByteArrayInputStream(bOut.toByteArray());
GridFSInputFile i = packageGridFs.createFile(packageAsStream);
i.setFilename(pageNo + "");
i.save();
} else {
if (gzipped) {
f.writeTo(out);
} else {
GZIPInputStream in = new GZIPInputStream(f.getInputStream());
byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
in.close();
}
}
} finally {
if (out != null) out.close();
if (packageOut != null) packageOut.close();
if (packageAsStream != null) packageAsStream.close();
}
}
示例13: testCreateGridFS
import com.mongodb.gridfs.GridFSDBFile; //導入方法依賴的package包/類
@Test
public void testCreateGridFS() throws IOException {
String userId = "some-user-id";
// each user has their own space named after their userId
// gridfs can be accessed the classic way ... using .files directly
// first drop gridfs storage for user
DBCollection files = db.getCollection(userId + ".files");
files.drop();
DBCollection chunks = db.getCollection(userId + ".chunks");
chunks.drop();
// create root folder
files = db.getCollection(userId + ".files");
BasicDBObject dir = new BasicDBObject("filename", "").append("dir", true);
files.insert(dir);
// create subdir
dir = new BasicDBObject("filename", "subdir").append("parent", dir.get("_id")).append("dir", true);
files.insert(dir);
// create/write file in that subdir
GridFS fs = new GridFS(db, userId);
GridFSInputFile blob = fs.createFile(new SampleInputStream(1024 * 1024));
// meta data
blob.setFilename("some-file.txt");
blob.setContentType("text/plain");
blob.put("parent", dir.get("_id"));
//BasicDBObject meta = new BasicDBObject();
//blob.setMetaData(meta);
blob.save();
// now let's start at the root and navigate down to the file we created
// get root
DBObject found = files.findOne(new BasicDBObject("filename", "").append("parent", null).append("dir", true));
// list root children
DBCursor rs = files.find(new BasicDBObject("parent", found.get("_id")));
assertThat(rs.size()).isEqualTo(1);
while(rs.hasNext()) {
found = rs.next();
}
assertThat(found.get("dir")).isEqualTo(true);
assertThat(found.get("filename")).isEqualTo("subdir");
// list subdir children
BasicDBObject dirId = new BasicDBObject("parent", found.get("_id"));
rs = files.find(dirId);
assertThat(rs.size()).isEqualTo(1);
while(rs.hasNext()) {
found = rs.next();
}
assertThat(found.get("dir")).isNull();
// read file
GridFSDBFile file = fs.findOne(new BasicDBObject("_id", found.get("_id")));
CountOutputStream out = new CountOutputStream();
file.writeTo(out);
assertThat(out.getCount()).isEqualTo(1024 * 1024);
// delete file
fs.remove(new BasicDBObject("_id", found.get("_id")));
// make sure that listing parent does not return the file any more
rs = files.find(dirId);
assertThat(rs.size()).isEqualTo(0);
}