- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 servlet,我首先在其中下载 pdf http://www.cbwe.gov.in/htmleditor1/pdf/sample.pdf将其内容上传到我的 blobstore 上,当用户在浏览器中发送 get 请求时,将在浏览器中下载 blob,但它不会下载,而是以其他格式显示数据。这是我的 servlet 代码:
package org.ritesh;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.ByteBuffer;
import javax.servlet.http.*;
import org.apache.commons.io.IOUtils;
import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.files.AppEngineFile;
import com.google.appengine.api.files.FileServiceFactory;
import com.google.appengine.api.files.FileService;
import com.google.appengine.api.files.FileWriteChannel;
@SuppressWarnings("serial")
public class BlobURLServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType("text/plain");
resp.getWriter().println("Hello, world");
FileService fileService = FileServiceFactory.getFileService();
// Create a new Blob file with mime-type "text/plain"
String url="http://www.cbwe.gov.in/htmleditor1/pdf/sample.pdf";
URL url1=new URL(url);
HttpURLConnection conn=(HttpURLConnection) url1.openConnection();
String content_type=conn.getContentType();
InputStream stream =conn.getInputStream();
AppEngineFile file = fileService.createNewBlobFile("application/pdf");
file=new AppEngineFile(file.getFullPath());
Boolean lock = true;
FileWriteChannel writeChannel = fileService.openWriteChannel(file, lock);
// This time we write to the channel directly
String s1="";
String s2="";
byte[] bytes = IOUtils.toByteArray(stream);
writeChannel.write(ByteBuffer.wrap(bytes));
writeChannel.closeFinally();
BlobKey blobKey = fileService.getBlobKey(file);
BlobstoreService blobStoreService = BlobstoreServiceFactory.getBlobstoreService();
blobStoreService.serve(blobKey, resp);
}
}
我在 onemoredemo1.appspot.com 上部署了这个 servlet。请打开此 url,并注意当您单击 BlobURL servlet 时,它显示内容而不是显示下载对话框。我应该在代码中进行哪些修改才能在浏览器中显示下载对话框?
最佳答案
看这里:
resp.setContentType("text/plain");
您说过内容是纯文本,但事实并非如此。您需要将 Content-Disposition
header 适当设置为附件,并将内容类型设置为 application/pdf
。
此外,如果您要提供二进制内容,您不应该也使用编写器(您正在使用编写器编写“Hello,world”
) .
如果您将前几行更改为:
resp.setContentType("application/pdf");
resp.setHeader("Content-Disposition", "attachment;filename=sample.pdf");
您可能会发现这就是所需要的全部。
关于java - blobStoreService.serve() 不提供下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11912759/
我有一个 servlet,我首先在其中下载 pdf http://www.cbwe.gov.in/htmleditor1/pdf/sample.pdf将其内容上传到我的 blobstore 上,当用户
在我的 Google App Engine 应用程序中,登录用户可以上传他们的照片及其其他详细信息。我使用 HttpSession 来管理日志记录。当登录用户填写表单并选择要上传的照片并单击“保存”按
自从 2 天谷歌搜索以来,我必须使用 java 在 Google 云存储中上传文件。现在我在使用 java 从 Google Cloud Storage 下载相同的文件时遇到了麻烦。 我尝试使用 Bl
我正在使用 Google 应用引擎和 Google 云存储在 GCS 中上传文件。我在这里创建上传 url。为了存储文件,我正在创建一个 url 以在 GCS 中托管该文件。我的问题是我有一个包含多个
我的问题类似于getting blobstore to callback to endpoint method但她/他没有得到回复。我实际上也写了我的代码。我将回调 url 创建为 Blobstore
我正在尝试从存储在 Google Cloud Storage 上的文件中读取字节并将其通过 HTTP POST 请求发送,但我在使用较大的文件时出现此异常,代码正在运行小文件: 此行抛出异常: wri
这是我的 Serve.java,这是我的 Serve servlet。 public class Serve extends HttpServlet { private BlobstoreServic
我正在使用 Java 开发 Appengine 应用程序,它允许用户上传通过名为 PaintWeb 的 HTML5 Canvas 库绘制的图像( http://code.google.com/p/pa
我在 GCS 中遇到问题。在我的应用程序中,我尝试使用带有 appengine sdk 1.9.0 的 BlobstoreService.generateUploadUrl() 从托管在 Google
我是一名优秀的程序员,十分优秀!