我目前正在使用 TrueZip 将文件添加到通过 MultiPartFile 上传到服务器的 Zip 文件中。
问题 附加文件后,zip 变得无效。它不能再作为 zip 文件打开。
代码 让我们从我的上传控制器中的相关代码开始(文件是 MultiPartFile):
    // Get the file
    File dest = null;
    TFile zip = null;
    try {
        // Obtain the file locally, zip, and delete the old
        dest = new File(request.getRealPath("") + "/datasource/uploads/" + fixedFileName);
        file.transferTo(dest);
        // Validate
        zip = new TFile(dest);
        resp = mls.validateMapLayer(zip);
        // Now perform the upload and delete the temp file
        FoundryUserDetails userDetails = (FoundryUserDetails) SecurityContextHolder.getContext().getAuthentication()
                .getPrincipal();
        UserIdentity ui = userDetails.getUserIdentity();
        MapLayer newLayer = new MapLayer();
                    // generate the prj
        mls.generateProjection(resp, dest.getAbsolutePath(), projection);
方法“generateProjection”是添加文件的地方:
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();
    }
}
当然,这很好用。
问题
有谁知道为什么在将 MultiPartFile 复制到本地文件的 Web 服务器中,TFileOutputStream 无法正确写入?