阅读背景:

在gae / j中的FileService api中设置文件名

来源:互联网 

i wrote a servlet which is uploading blobs successfully but it is uploading with no name how to set the name of uploaded file in the servlet

我写了一个servlet,它正在成功上传blob,但它上传时没有名字如何在servlet中设置上传文件的名称

here is segment of servlet code

这是servlet代码的一部分

public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {



         FileService fileService = FileServiceFactory.getFileService();

          // Create a new Blob file with mime-type "text/plain"

          String url="https://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();

2 个解决方案

#1


0  

Pass filename as a second parameter to fileService.createNewBlobFile("application/pdf", "filename.pdf").

将filename作为第二个参数传递给fileService.createNewBlobFile(“application / pdf”,“filename.pdf”)。

#2


0  

You can get file name from your URL

您可以从您的URL获取文件名

String fileNameWithoutExtn = url.substring(0, url.lastIndexOf('.'));

String fileNameWithoutExtn = url.substring(0,url.lastIndexOf('。'));

Then pass fileName as a parameter

然后将fileName作为参数传递

FileWriteChannel writeChannel = fileService.openWriteChannel(file, lock,fileNameWithoutExtn );

分享到: