【问题标题】:Setting name of file in FileService api in gae/j在 gae/j 中的 FileService api 中设置文件名
【发布时间】:2012-08-11 10:16:30
【问题描述】:

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

这是一段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="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();

【问题讨论】:

    标签: java google-app-engine blobstore


    【解决方案1】:

    将文件名作为第二个参数传递给fileService.createNewBlobFile("application/pdf", "filename.pdf")

    【讨论】:

      【解决方案2】:

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

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

      然后将fileName作为参数传递

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

      【讨论】:

      • openWriteChannel(..) 不带三个参数。
      猜你喜欢
      • 1970-01-01
      • 2011-02-01
      • 2011-07-25
      • 1970-01-01
      • 2015-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多