简体   繁体   English

是否可以在Java中将.swf文件转换为base64格式

[英]is it possible to convert .swf file to base64 format in java

I am trying to load a .swf file in my page, i would like to make this load faster by converting it to Base64, rather providing a src. 我正在尝试在页面中加载.swf文件,我想通过将其转换为Base64而不是提供src来加快加载速度。 This is working great with image formats by using the below code 通过使用以下代码,这非常适用于图像格式

Java code Java代码

BufferedImage buffImg = ImageIO.read(new File(imagePath));
ImageIO.write(buffImg, imgExtension, bos);
byte[] imageBytes = bos.toByteArray();                  
BASE64Encoder encoder = new BASE64Encoder();    
imageString = encoder.encode(imageBytes);

but this is not working for swf file. 但这不适用于swf文件。 is there any possible way to achieve this. 有什么可能的方法来实现这一目标。

Html HTML

<object width="10" height="10" data="data:application/x-shockwave-flash;base64, RldTCSEAAABIAZAAZAAADAEARBEIAAAAQwIAAP9AAAAA"></object>

thanks in advance. 提前致谢。

Trying to get the file in base64 will not speed up the file transfer, it's just the opposite as it will convert the file which is stored in bytes (base256 if it can be said that way) to base64 (64 printable characters), so the final amount of data you will be transfering is more. 尝试在base64中获取文件不会加快文件传输速度,相反,因为它将存储在字节中的文件(如果可以这样说,则为base256)转换为base64(64个可打印字符),因此您将传输的最终数据量更多。

The only "win" is that you might be able to load it as part of the page instead of the browser making another call for the swf file, which should be no issue on http 1.1. 唯一的“胜利”是,您可以将其作为页面的一部分加载,而无需浏览器再次调用swf文件,这在http 1.1上应该没有问题。

Unless you have some other good reason to do this, I would not suggest this kind of practice. 除非您有其他充分的理由这样做,否则我不建议您采用这种做法。

If you have your swf file(s) in a database as a blob, you could just make a servlet which sets the proper contenttype and write the whole file with the ServletOutputStream, without any tags. 如果您在数据库中将swf文件作为Blob存放,则可以制作一个Servlet,该Servlet设置适当的内容类型,并使用ServletOutputStream写入整个文件,而无需任何标签。 In your html code, you would have to reference to the servlet instead of a fixed file. 在您的html代码中,您将不得不引用servlet而不是固定文件。

If you still want to convert the file to base64, you shouldn't use some image API, but get the file in a standard way for binary files, here's a sample that should do the job: 如果仍然要将文件转换为base64,则不应该使用某些图像API,而应以标准方式获取二进制文件的文件,以下是一个应做的示例:

http://www.javapractices.com/topic/TopicAction.do?Id=245 http://www.javapractices.com/topic/TopicAction.do?Id=245

You can still do the encoding as you did it once you have a byte array: 一旦拥有字节数组,仍然可以像进行编码一样进行编码:

File file = new File(imagePath);
log("File size: " + file.length());
byte[] result = null;
try {
  InputStream input =  new BufferedInputStream(new FileInputStream(file));
  result = readAndClose(input);
}
catch (FileNotFoundException ex){
  log(ex);
}
BASE64Encoder encoder = new BASE64Encoder();    
imageString = encoder.encode(result);

And the readAndClose method: 和readAndClose方法:

byte[] readAndClose(InputStream aInput){
  byte[] bucket = new byte[32*1024]; 
  ByteArrayOutputStream result = null; 
  try  {
    try {
      result = new ByteArrayOutputStream(bucket.length);
      int bytesRead = 0;
      while(bytesRead != -1){
        bytesRead = aInput.read(bucket);
        if(bytesRead > 0){
          result.write(bucket, 0, bytesRead);
        }
      }
    }
    finally {
      aInput.close();
    }
  }
  catch (IOException ex){
    log(ex);
  }
  return result.toByteArray();
}

This should do the trick, maybe some fine tunings to adapt the code to your specific situation, optimize it and better error handling... 这应该可以解决问题,也许可以进行一些微调以使代码适应您的特定情况,对其进行优化并更好地进行错误处理...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM