【发布时间】:2019-01-13 14:18:15
【问题描述】:
我一直在尝试使用我的应用程序在 Google Cloud Storage 上上传 pdf 文件,但我无法这样做。我在网上搜索并找到了以下给出的代码,但问题是此代码在上传 txt 时很有用文件但不是 pdf 文件。当我尝试上传 pdf 文件时,它会成功上传,但当我尝试打开它时,它不会打开。我正在使用以下代码:
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
RequestDispatcher rd=req.getRequestDispatcher("career-registration-successfull.jsp");
RequestDispatcher rd1=req.getRequestDispatcher("career-registration-failed.jsp");
String name="";
String email="";
long whatsapp=0;
long number=0;
String query="";
int flag1=0;
int flag2=0;
isMultipart = ServletFileUpload.isMultipartContent(req);
resp.setContentType("text/html");
java.io.PrintWriter out = resp.getWriter( );
FileInputStream fileInputStream = new FileInputStream("C:\\Users\\Subhanshu Bigasia\\Desktop\\Amazon.pdf");
Storage storage = StorageOptions.getDefaultInstance().getService();
Bucket bucket=storage.get(("combucket1eduvitae7"));
ServletFileUpload sfu = new ServletFileUpload(new DiskFileItemFactory());
String targetFileStr="";
try {
List<FileItem> fileName = sfu.parseRequest(req);
for(FileItem f:fileName)
{
try {
f.write (new File("C:\\Users\\Subhanshu Bigasia\\Desktop\\Afcat.pdf"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//targetFileStr = readFile("/Users/tkmajdt/Documents/workspace/File1POC1/" + f.getName(),Charset.defaultCharset());
targetFileStr = new String(Files.readAllBytes(Paths.get("C:\\Users\\Subhanshu Bigasia\\Desktop\\Afcat.pdf")));
}
}
catch(Exception e) {
e.printStackTrace();
}
BlobId blobId = BlobId.of("combucket1eduvitae", "my_blob_name1");
//Blob blob = bucket.create("my_blob_name1", targetFileStr.getBytes(), "text/plain");
Blob blob=bucket.create("myblob",fileInputStream, "text");
谁能帮忙解决这个问题。
提前谢谢你
【问题讨论】:
标签: java eclipse file-upload google-cloud-platform google-cloud-storage