1

使用 org.apache.wicket.util.resource.IResourceStream 我在服务器上创建了 zip 文件,并在 outputstream 中写入了相同的 zip 文件。但它会引发以下错误,我正在打破我的头脑。有什么建议吗?

Timestamp: 7/25/2012 3:13:28 PM
Error: not well-formed
Source File: XXX
Line: 1, Column: 3
Source Code:
PK

Java 代码

        AjaxButton one = new AjaxButton("one"){     
                @Override
                public void onSubmit(AjaxRequestTarget target,Form form) {
                    // TODO Auto-generated method stub
                    try {

                        {
                        IResourceStream iResourceStream  = null;
                        iResourceStream = new AbstractResourceStreamWriter(){

                            @Override
                            public String getContentType() {
                                // TODO Auto-generated method stub
                                return "application/zip";
                            }

                            @Override
                            public void write(OutputStream output) {
                                // TODO Auto-generated method stub
                                File tmpFile = null;
                                String batchFileName = "batch_"+dateFormat.format(new Date())".zip";
                                File zipFile = new File(batchFileName);
                                FileOutputStream zipFileOutputStream = null;
                                ZipOutputStream zipOutputStream = null;
                                try
                                {
                                    zipFileOutputStream = new FileOutputStream(zipFile);
                                    zipOutputStream = 
                                        new ZipOutputStream(zipFileOutputStream);    
                                    zipOutputStream.setLevel(Deflater.DEFAULT_COMPRESSION);
                                    for(XXX)
                                    {
// create tmpFile zip file here                                 
                                                    ZipEntry zipAdd = new ZipEntry(tmpFile.getName());  
                                                    System.out
                                                            .println(tmpFile.getName());
                                                    zipOutputStream.putNextEntry(zipAdd);
                                                    zipOutputStream.write(IOUtils.toByteArray(new FileInputStream(tmpFile)));
                                                    zipOutputStream.closeEntry();
                                                }
                                            }
                                        }
                                    }

                                }
                                catch (Exception e) {
                                    // TODO: handle exception
                                    e.printStackTrace();
                                }
                                finally
                                {
                                    if(zipOutputStream != null){
                                        try {
                                            zipOutputStream.flush();
                                            zipOutputStream.close();
                                        } catch (IOException e) {                   
                                            e.printStackTrace();
                                        }
                                    }

                                    if(zipFileOutputStream != null){
                                        try {
                                            zipFileOutputStream.flush();
                                            zipFileOutputStream.close();
                                        } catch (IOException e) {                   
                                            e.printStackTrace();
                                        }
                                    }
                                    try
                                    {
                                        {
                                            InputStream in = new FileInputStream(zipFile);
                                            byte[] buf = new byte[1024];
                                            int len;
                                            while ((len = in.read(buf)) > 0){
                                                output.write(buf, 0, len);
                                            }
                                            in.close();
                                            output.close();
                                        }
                                    }
                                    catch (Exception e) {
                                        // TODO: handle exception
                                        e.printStackTrace();
                                    }
                                }


                            }

                        };

                        getRequestCycle()
                        .setRequestTarget(new ResourceStreamRequestTarget(iResourceStream)
                        .setFileName("batch.zip"));


                    } else {

                    }

                } catch (Exception e)
{
}
}
4

1 回答 1

2

您正在执行 AJAX 请求,但不是发送回 XML(浏览器中的 JS 代码所期望的),而是发送二进制数据。这就是为什么您会收到“格式不正确”的错误 - 它不是格式正确的 xml。

有两种方法可以完成这项工作。一种是根本不进行 AJAX 提交并使用常规 Button而不是AjaxButton. 我推荐这个。

如果你需要做一些其他的 AJAX 工作(更新面板或类似的东西)然后想要展示下载,看看这个:https ://cwiki.apache.org/WICKET/ajax-update-and-文件下载-in-one-blow.html

于 2012-07-25T10:39:35.413 回答