本文整理汇总了Java中com.google.appengine.api.blobstore.BlobInfoFactory类的典型用法代码示例。如果您正苦于以下问题:Java BlobInfoFactory类的具体用法?Java BlobInfoFactory怎么用?Java BlobInfoFactory使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
BlobInfoFactory类属于com.google.appengine.api.blobstore包,在下文中一共展示了BlobInfoFactory类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: serveDownload
import com.google.appengine.api.blobstore.BlobInfoFactory; //导入依赖的package包/类
/**
* Serves the attachment with cache control.
*
* @param req Only used to check the If-Modified-Since header.
*/
public void serveDownload(AttachmentId id,
HttpServletRequest req, HttpServletResponse resp) throws IOException {
if (maybeCached(req, resp, "download, id=" + id)) {
return;
}
AttachmentMetadata metadata = getMetadata(id);
if (metadata == null) {
throw NotFoundException.withInternalMessage("Attachment id unknown: " + id);
}
BlobKey key = metadata.getBlobKey();
BlobInfo info = new BlobInfoFactory().loadBlobInfo(key);
String disposition = "attachment; filename=\""
// TODO(ohler): Investigate what escaping we need here, and whether the
// blobstore service has already done some escaping that we need to undo
// (it seems to do percent-encoding on " characters).
+ info.getFilename().replace("\"", "\\\"").replace("\\", "\\\\")
+ "\"";
log.info("Serving " + info + " with Content-Disposition: " + disposition);
resp.setHeader("Content-Disposition", disposition);
blobstore.serve(key, resp);
}
示例2: assignBlobKey
import com.google.appengine.api.blobstore.BlobInfoFactory; //导入依赖的package包/类
/**
* Returns a new blobkey for an uploaded blob.
* If no blob was uploaded, then null is returned.
* @param req
* : the HTTP Servlet Request from the Servlet
* @param paramName
* : the name of the HTTP blob parameter
* @param blobStoreService
* : the blobstore service initialized in the calling servlet
* @return a new BlobKey for this blob
*/
public static BlobKey assignBlobKey(HttpServletRequest req, String paramName,
BlobstoreService blobstoreService) {
java.util.Map<String, List<BlobKey>> blobs = blobstoreService.getUploads(req);
List<BlobKey> blobKeys = blobs.get(paramName);
if (blobKeys != null) {
BlobKey blobKey = blobKeys.get(0);
final BlobInfo blobInfo = new BlobInfoFactory().loadBlobInfo(blobKey);
if (blobKey != null) {
if (blobInfo.getSize() > 0) {
return blobKey;
}
else {
blobstoreService.delete(blobKey);
return null;
}
}
else {
return null;
}
}
else {
return null;
}
}
示例3: doPost
import com.google.appengine.api.blobstore.BlobInfoFactory; //导入依赖的package包/类
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String title = req.getParameter("title");
Map<String, List<BlobKey>> blobs = blobstoreService.getUploads(req);
BlobKey blobKey = blobs.get("myFile").get(0);
BlobInfo info = new BlobInfoFactory().loadBlobInfo(blobKey);
if (blobKey == null) {
resp.sendRedirect("/admin/resource/add.jsp?error");
}
Resource resource = new Resource();
resource.setTitle(title);
resource.setBlobKey(blobKey);
resource.setContentType(info.getContentType());
resource.setFileName(info.getFilename());
resource.setSize(info.getSize());
PersistenceManager pm = PMF.get().getPersistenceManager();
pm.makePersistent(resource);
pm.close();
resp.sendRedirect("/admin/resource/");
}
示例4: getFileInfo
import com.google.appengine.api.blobstore.BlobInfoFactory; //导入依赖的package包/类
@Override
public FileInfo getFileInfo(HttpServletRequest req) {
Map<String, BlobKey> blobs = blobstoreService.getUploadedBlobs(req);
if (blobs.keySet().isEmpty()) {
throw new RuntimeException("No File found");
}
Iterator<String> names = blobs.keySet().iterator();
String blobName = names.next();
BlobKey blobKey = blobs.get(blobName);
BlobInfoFactory blobInfoFactory = new BlobInfoFactory();
BlobInfo blobInfo = blobInfoFactory.loadBlobInfo(blobKey);
FileInfo fileInfo = new FileInfo();
fileInfo.setFileName(blobInfo.getFilename());
fileInfo.setContentType(blobInfo.getContentType());
fileInfo.setSize(blobInfo.getSize());
return fileInfo;
}
示例5: RawAttachmentService
import com.google.appengine.api.blobstore.BlobInfoFactory; //导入依赖的package包/类
@Inject
public RawAttachmentService(BlobstoreService blobstoreService, ImagesService imagesService,
BlobInfoFactory blobInfoFactory,
@Flag(FlagName.ATTACHMENT_HEADER_BYTES_UPPER_BOUND) int headerBytesUpperBound,
RandomBase64Generator random64,
MetadataDirectory metadataDirectory) {
this.blobstoreService = blobstoreService;
this.imagesService = imagesService;
this.blobInfoFactory = blobInfoFactory;
this.headerBytesUpperBound = headerBytesUpperBound;
this.random64 = random64;
this.metadataDirectory = metadataDirectory;
}
示例6: assignBlobKeys
import com.google.appengine.api.blobstore.BlobInfoFactory; //导入依赖的package包/类
/**
* Returns a list of blobkeys for multiple blob uploads.
* If no blob was uploaded, then an empty list is returned.
* @param req
* : the HTTP Servlet Request from the Servlet
* @param paramName
* : the name of the HTTP blob parameter
* @param blobStoreService
* : the blobstore service initialized in the calling servlet
* @return a list of BlobKeys
*/
public static ArrayList<BlobKey> assignBlobKeys(HttpServletRequest req, String paramName,
BlobstoreService blobstoreService) {
ArrayList<BlobKey> finalBlobKeys = new ArrayList<BlobKey>();
java.util.Map<String, List<BlobKey>> blobs = blobstoreService.getUploads(req);
List<BlobKey> blobKeys = blobs.get(paramName);
if (blobKeys != null) {
for (BlobKey blobKey : blobKeys) {
final BlobInfo blobInfo = new BlobInfoFactory().loadBlobInfo(blobKey);
if (blobKey != null) {
if (blobInfo.getSize() > 0) {
finalBlobKeys.add(blobKey);
}
else {
blobstoreService.delete(blobKey);
}
}
}
}
else {
return null;
}
return finalBlobKeys;
}
示例7: readBlobFully
import com.google.appengine.api.blobstore.BlobInfoFactory; //导入依赖的package包/类
public static byte[] readBlobFully(BlobKey blobKey) {
BlobstoreService blobstoreService = BlobstoreServiceFactory
.getBlobstoreService();
BlobInfo blobInfo = new BlobInfoFactory().loadBlobInfo(blobKey);
if (blobInfo == null)
return null;
if (blobInfo.getSize() > Integer.MAX_VALUE)
throw new RuntimeException(
"This method can only process blobs up to "
+ Integer.MAX_VALUE + " bytes");
int blobSize = (int) blobInfo.getSize();
int chunks = (int) Math
.ceil(((double) blobSize / BlobstoreService.MAX_BLOB_FETCH_SIZE));
int totalBytesRead = 0;
int startPointer = 0;
int endPointer;
byte[] blobBytes = new byte[blobSize];
for (int i = 0; i < chunks; i++) {
endPointer = Math.min(blobSize - 1, startPointer
+ BlobstoreService.MAX_BLOB_FETCH_SIZE - 1);
byte[] bytes = blobstoreService.fetchData(blobKey, startPointer,
endPointer);
for (int j = 0; j < bytes.length; j++)
blobBytes[j + totalBytesRead] = bytes[j];
startPointer = endPointer + 1;
totalBytesRead += bytes.length;
}
return blobBytes;
}
示例8: getSize
import com.google.appengine.api.blobstore.BlobInfoFactory; //导入依赖的package包/类
public long getSize() {
BlobKey key = getKey();
if (key == null)
return 0;
BlobInfo info = new BlobInfoFactory().loadBlobInfo(key);
if (info == null)
return 0;
return info.getSize();
}
示例9: provideBlobInfoFactory
import com.google.appengine.api.blobstore.BlobInfoFactory; //导入依赖的package包/类
@Provides
BlobInfoFactory provideBlobInfoFactory() {
return new BlobInfoFactory();
}
示例10: doGet
import com.google.appengine.api.blobstore.BlobInfoFactory; //导入依赖的package包/类
/**
* Respond to servlet GET requests
*/
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
// Lets check the action required by the jsp
String message = req.getParameter("msg");
String action = req.getParameter("action");
if (message.equals("success")) {
// We return information about the file in JSON format
if (action.equalsIgnoreCase("upload")) {
String type = req.getParameter("type");
String datastoreObjectKeyString =
req.getParameter("datastoreObjectKey");
Key datastoreObjectKey = KeyFactory.stringToKey(datastoreObjectKeyString);
BlobKey fileKey = null;
String fileKeyString = null;
String jsonString = null;
// Check type
if (type.equalsIgnoreCase("audio_music") ||
type.equalsIgnoreCase("audio_voice")) {
StationAudio stationAudio =
StationAudioManager.getStationAudio(datastoreObjectKey);
fileKey = stationAudio.getStationAudioMultimediaContent();
}
else if (type.equalsIgnoreCase("image")) {
StationImage stationImage =
StationImageManager.getStationImage(datastoreObjectKey);
fileKey = stationImage.getStationImageMultimediaContent();
}
fileKeyString = fileKey.getKeyString();
BlobInfoFactory bif = new BlobInfoFactory();
BlobInfo blobInfo = bif.loadBlobInfo(fileKey);
String fileName = blobInfo.getFilename();
jsonString = "{" + "\n" +
"\"datastoreObjectKey\":" + "\"" + datastoreObjectKeyString + "\"," + "\n" +
"\"fileKey\":" + "\"" + fileKeyString + "\"," + "\n" +
"\"fileName\":" + "\"" + fileName + "\"\n" +
"}";
log.info("jsonString: " + jsonString);
resp.setContentType("application/json;charset=UTF-8");
resp.setCharacterEncoding("UTF-8");
resp.getWriter().println(jsonString);
}
else if (action.equals("delete")){
resp.getWriter().println("File deleted successfully.");
}
}
}
示例11: getFileSize
import com.google.appengine.api.blobstore.BlobInfoFactory; //导入依赖的package包/类
private static long getFileSize(BlobKey blobKey) {
BlobInfoFactory blobInfoFactory = new BlobInfoFactory();
return blobInfoFactory.loadBlobInfo(blobKey).getSize();
}
示例12: finishObjectCreation
import com.google.appengine.api.blobstore.BlobInfoFactory; //导入依赖的package包/类
@Override
public void finishObjectCreation(RawGcsCreationToken token, ByteBuffer chunk, long timeoutMillis)
throws IOException {
ensureInitialized();
Token t = append(token, chunk);
int totalBytes = 0;
BlobKey blobKey = getBlobKeyForFilename(t.filename);
try (WritableByteChannel outputChannel = Channels.newChannel(blobStorage.storeBlob(blobKey))) {
for (ByteBuffer buffer : inMemoryData.get(t.filename)){
totalBytes += buffer.remaining();
outputChannel.write(buffer);
}
inMemoryData.remove(t.filename);
}
String mimeType = t.options.getMimeType();
if (Strings.isNullOrEmpty(mimeType)) {
mimeType = "application/octet-stream";
}
BlobInfo blobInfo = new BlobInfo(
blobKey, mimeType, new Date(), getPathForGcsFilename(t.filename),
totalBytes);
String namespace = NamespaceManager.get();
try {
NamespaceManager.set("");
String blobKeyString = blobInfo.getBlobKey().getKeyString();
Entity blobInfoEntity =
new Entity(GOOGLE_STORAGE_FILE_KIND, blobKeyString);
blobInfoEntity.setProperty(BlobInfoFactory.CONTENT_TYPE, blobInfo.getContentType());
blobInfoEntity.setProperty(BlobInfoFactory.CREATION, blobInfo.getCreation());
blobInfoEntity.setProperty(BlobInfoFactory.FILENAME, blobInfo.getFilename());
blobInfoEntity.setProperty(BlobInfoFactory.SIZE, blobInfo.getSize());
datastore.put(blobInfoEntity);
} finally {
NamespaceManager.set(namespace);
}
Entity e = new Entity(makeKey(t.filename));
ByteArrayOutputStream bout = new ByteArrayOutputStream();
try (ObjectOutputStream oout = new ObjectOutputStream(bout)) {
oout.writeObject(t.options);
}
e.setUnindexedProperty(OPTIONS_PROP, new Blob(bout.toByteArray()));
e.setUnindexedProperty(CREATION_TIME_PROP, System.currentTimeMillis());
e.setUnindexedProperty(FILE_LENGTH_PROP, totalBytes);
datastore.put(null, e);
}
示例13: getBlobInfoFactory
import com.google.appengine.api.blobstore.BlobInfoFactory; //导入依赖的package包/类
@Provides
@Singleton
BlobInfoFactory getBlobInfoFactory() {
return new BlobInfoFactory();
}
示例14: doPost
import com.google.appengine.api.blobstore.BlobInfoFactory; //导入依赖的package包/类
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws
IOException {
UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser();
Map<String, BlobKey> blobs = blobstoreService.getUploadedBlobs(req);
if (blobs.keySet().isEmpty()) {
resp.sendRedirect("/?error=" +
URLEncoder.encode("No file uploaded", "UTF-8"));
return;
}
Iterator<String> names = blobs.keySet().iterator();
String blobName = names.next();
BlobKey blobKey = blobs.get(blobName);
if (user == null) {
blobstoreService.delete(blobKey);
resp.sendRedirect("/?error=" +
URLEncoder.encode("Must be logged in to upload", "UTF-8"));
return;
}
BlobInfoFactory blobInfoFactory = new BlobInfoFactory();
BlobInfo blobInfo = blobInfoFactory.loadBlobInfo(blobKey);
String contentType = blobInfo.getContentType();
long size = blobInfo.getSize();
Date creation = blobInfo.getCreation();
String fileName = blobInfo.getFilename();
String title = req.getParameter("title");
String description = req.getParameter("description");
boolean isShared = "public".equalsIgnoreCase(req.getParameter("share"));
try {
MediaObject mediaObj = new MediaObject(user, blobKey, creation,
contentType, fileName, size, title, description, isShared);
PMF.get().getPersistenceManager().makePersistent(mediaObj);
resp.sendRedirect("/");
} catch (Exception e) {
blobstoreService.delete(blobKey);
resp.sendRedirect("/?error=" +
URLEncoder.encode("Object save failed: " + e.getMessage(), "UTF-8"));
}
}
示例15: doGet
import com.google.appengine.api.blobstore.BlobInfoFactory; //导入依赖的package包/类
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException {
BlobInfoFactory bif = new BlobInfoFactory();
BlobKey blobKey = new BlobKey(req.getParameter("file_id"));
String fileName = bif.loadBlobInfo(blobKey).getFilename();
res.setContentType("text/plain");
res.setHeader("Content-Disposition",
"attachment; filename=" + fileName);
blobstoreService.serve(blobKey, res);
}