使用org.apache.wicket.util.resource.IResourceStream我在服务器和输出流中创建了zip文件,编写了相同的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)
{
}
}
答案 0 :(得分:2)
您正在执行AJAX请求,但不是发回XML(浏览器期望的JS代码),而是发送二进制数据。这就是为什么你得到“格式不正确”的错误 - 它不是格式良好的xml。
有两种方法可以完成这项工作。一种是简单地不进行AJAX提交并使用常规Button
而不是AjaxButton
。我推荐这个。
如果您需要执行其他一些AJAX工作(更新面板或类似内容),然后想要提供下载,请查看:https://cwiki.apache.org/WICKET/ajax-update-and-file-download-in-one-blow.html