3

我想实现这个页面的例子。

我被困在getResourceStream()方法上。我的应用程序有一个byte[](生成的 XML 文件),用户应该能够下载。问题是我不知道如何将 a 转换byte[]IResourceStream.

这就是我所拥有的:

final AJAXDownload download = new AJAXDownload()
{
    private static final long serialVersionUID = 1L;

    @Override
    protected IResourceStream getResourceStream()
    {
        ByteArrayResource bar = new ByteArrayResource("TEXT??", xmlFileInBytes);
                return (IResourceStream) bar;
        }
    };

上面的代码给出了一个ClassCastException. 有人可以解释我该如何解决这个问题吗?

4

3 回答 3

6

You can use the class StringResourceStream which is a child of IResourceStream and to use the byte array:

You can do something like this:

  byte byteArray[] = new byte[1024]   //this is the byte array containing the xml which you want to use.
  StringResourceStream srs = new StringResourceStream(new String(byteArray));

So in your case the method will look something like below:

 final AJAXDownload download = new AJAXDownload()
 {
     private static final long serialVersionUID = 1L;

     @Override
     protected IResourceStream getResourceStream()
     {
         StringResourceStream bar = new StringResourceStream (new String(xmlFileInBytes) );
                 return (IResourceStream) bar;
         }
     };
于 2013-04-15T09:01:08.130 回答
2

该类ByteArrayResource实现IResource,但不是IResourceStream。这就是为什么你有ClassCastException.

要解决它,您应该找到一个实现的类IResourceStream;查看它的Javadoc以获取可能的实现列表。

于 2013-04-15T08:59:06.190 回答
0

这是目前未经测试的代码,但不是做

ByteArrayResource bar = new ByteArrayResource("TEXT??", xmlFileInBytes);
return (IResourceStream) bar;

你可以尝试getResourceStream如下:

ByteArrayResource bar = new ByteArrayResource("TEXT??", xmlFileInBytes);
return bar.getResourceStream();
于 2013-12-25T08:25:30.950 回答