dspace批量导入编码问题

先看代码:


package org.dspace.createarchive;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;


public class GenFile {


/**
* 创建文件.

* @author Administrator
* @deprecated.

*/
void genOneFile(String content, String name, String path, int gb) {
FileOutputStream fos;
try {
fos = new FileOutputStream(path + File.separator + name);
if (gb == 1) {
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8"); //dublin_core.xml必须为UTF-8编码
osw.write(content);
osw.flush();
osw.close();
} else {
OutputStreamWriter osw = new OutputStreamWriter(fos); //contents、license.txt为本地默认编码
osw.write(content);
osw.flush();
osw.close();
}


} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}


/**
* 创建Dspace导入所需数据文件.

* @author Administrator
* @deprecated.

*/
public static int runGenfile(String sourPath, String desPath,
String xmlPath, String xlsPath, String tPath, String sheetName) {
String xmlStr = "";
String bookName = "";
try {
if (sourPath.equals("error") || desPath.equals("error")
|| xmlPath.equals("error")) {
return 0;
} else {
GenFile g = new GenFile();
File f = new File(sourPath);
String[] names = f.list();
for (int i = 0; i < names.length; ++i) {
// System.out.println(names[i]);
bookName = names[i].substring(0, names[i].lastIndexOf("."));
xmlStr = ReadDC.GenXML(xmlPath, xlsPath, sheetName,
bookName);
if (xmlStr.length() > 5) {
String fileName = String.valueOf(i);
String filespath = desPath + File.separator + fileName;
File out = new File(filespath);
out.mkdir();
g.genOneFile(names[i] + "\tbundle:ORIGINAL",
"contents", filespath, 0); // 生成“contents”文件


g.genOneFile(ReadDC.GenTxt(tPath), "license.txt",
filespath, 0); // 生成“license”文件


g.genOneFile(xmlStr, "dublin_core.xml", filespath, 1);// 生成“dublin_core.xml”文件,编码UTF-8


File sour = new File(sourPath + File.separator
+ names[i]);
File des = new File(desPath + File.separator + fileName
+ File.separator + names[i]);
sour.renameTo(des);
}
}
File file = new File(desPath);
if (file.list().length > 0) {
return 1;
} else {
return 0;
}
}
} catch (IOException e) {
e.printStackTrace();
return 0;
}
}

通过对dspace导出文件的分析,只要生成相应的导出文件结构及文件,即可把外部文件批量导入到dspace,其中编码问题尤其需要注意,一句话,“dublin_core.xml”需要UTF-8编码,其他文件本地默认编码即可。

你可能感兴趣的:(String,File,Class,Path)