使用 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)
{
}
}