Spring mvc TrueZip和多部分表单
我目前正在使用TrueZip将一个文件添加到通过MultiPartFile上传到服务器的Zip文件中 问题 附加文件后,zip将无效。它不能再作为zip文件打开 代码 让我们从我的上传控制器中的相关代码开始(文件是MultiPartFile): “generateProjection”方法用于添加文件:Spring mvc TrueZip和多部分表单,spring-mvc,truezip,Spring Mvc,Truezip,我目前正在使用TrueZip将一个文件添加到通过MultiPartFile上传到服务器的Zip文件中 问题 附加文件后,zip将无效。它不能再作为zip文件打开 代码 让我们从我的上传控制器中的相关代码开始(文件是MultiPartFile): “generateProjection”方法用于添加文件: public void generateProjection(UploadMapResponse resp, String fLoc, FoundryCRS proj) throws NoSuc
public void generateProjection(UploadMapResponse resp, String fLoc, FoundryCRS proj) throws NoSuchAuthorityCodeException,
FactoryException, IOException {
TFile projFile = new TFile(fLoc, resp.getLayerName() + ".prj");
CoordinateReferenceSystem crs = CRS.decode(proj.getEpsg());
String wkt = crs.toWKT();
TConfig config = TConfig.push();
try {
config.setOutputPreferences(config.getOutputPreferences().set(FsOutputOption.GROW));
TFileOutputStream writer = new TFileOutputStream(projFile);
try {
writer.write(wkt.getBytes());
} finally {
writer.close();
}
} finally {
config.close();
}
}
为了测试这是否有效,我在一个简单的main中进行了尝试:
public static void main(String[] args) {
File f = new File("C:/Data/SierritaDec2011TopoContours.zip");
TFile tf = new TFile(f);
tf.listFiles();
TFile proj = new TFile(f, "test.prj");
TConfig config = TConfig.push();
try {
config.setOutputPreferences(config.getOutputPreferences().set(FsOutputOption.GROW));
TFileOutputStream writer = null;
try {
writer = new TFileOutputStream(proj);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
try {
writer.write("Hello Zip world".getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} finally {
// Pop the current configuration off the inheritable thread local
// stack.
config.close();
}
}
当然,这很管用
问题
有人知道为什么在将多部分文件复制到本地文件的web服务器中,TFileOutputStream无法正确写入吗 在长时间运行的服务器应用程序中,您可能需要添加对TVFS.sync()或TVFS.umount()的调用,以便同步或umount归档文件。对于ZIP文件,这将触发在ZIP文件末尾写入中心目录,这是形成有效ZIP文件所必需的 请查看Javadoc以确定哪个调用最适合您的用例: 另外,请注意,在每次追加操作后调用TFVS.sync()或TVFS.umount()将导致每次写入的中心目录不断增加,这将导致巨大的开销。所以当你需要这么做的时候就值得考虑。一般来说,只有当您希望第三方访问ZIP文件时,才需要这样做。第三方是指不与TrueZIP内核交互以访问ZIP文件的任何人。这就是确切的解决方案(我使用了TVFS.unmount());
public static void main(String[] args) {
File f = new File("C:/Data/SierritaDec2011TopoContours.zip");
TFile tf = new TFile(f);
tf.listFiles();
TFile proj = new TFile(f, "test.prj");
TConfig config = TConfig.push();
try {
config.setOutputPreferences(config.getOutputPreferences().set(FsOutputOption.GROW));
TFileOutputStream writer = null;
try {
writer = new TFileOutputStream(proj);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
try {
writer.write("Hello Zip world".getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} finally {
// Pop the current configuration off the inheritable thread local
// stack.
config.close();
}
}