fileupload上传xls文件乱码。获取文件路径也获取不到
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
if (!TelComUtils.HasRole("130010001", request)) {
return mapping.findForward(TelComUtils.GloabalError);
}
String forward = "success";
request.setCharacterEncoding("GBK");
String bcause = null;
String textfield = null;
String batch_file = null;
String file_name = null;
int che = 0;
BatchIn batchIn = new BatchIn();
DiskFileItemFactory factory = new DiskFileItemFactory(); // 创建文件解析工厂
ServletFileUpload file = new ServletFileUpload(factory); // 文件上传解析器
// FileItemIterator fi = file.getItemIterator(request); //
// 从文件系统中的某个文件中获得输入字节
List items = file.parseRequest(request);// 解析二进制的request请求
Iterator iter = items.iterator();
List list=new ArrayList();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
// 检查是一个普通的表单域还是File组件
if( !item.isFormField() ){
//以下这段代码,如果上传的为xls文件,获取的值就是“ 邢唷??”开头的乱码,我试了各种转码都没办法。
batch_file = item.getString("GBK");
}
// //
// // // //item.getName 返回的是完整的文件名,
// //
// //
// // }else{
if ((item.getFieldName()).equals("batch_file")) {
file_name = item.getName();
}
if ((item.getFieldName()).equals("che")) {
che = Integer.parseInt(item.getString());
}
if ((item.getFieldName()).equals("bcause")) {
bcause = item.getString();
bcause = new String(bcause.getBytes("ISO-8859-1"), "GBK");
}
if (item.getFieldName().equals("textfield")) {
textfield = item.getString();
textfield = new String(textfield.getBytes("ISO-8859-1"), "GBK");
// System.out.println(descript);
// }
}
}
if (che == 0) {
textfield = null;
}
// FileItemStream fis= (FileItemStream) fi.next(); // 文件逐行向下读取
// String file_name = batch_file; // 获取文件的名字
file_name = file_name.substring(file_name.lastIndexOf("\\") + 1,
file_name.length());// 字符串的拼接
String file_postfix = file_name
.substring(file_name.lastIndexOf(".") + 1);// 获取上传文件的后缀
BathDAO baDao = new BathDAO();
if (baDao.isFileExists(file_name)) {
forward = "fail";
String msg = "<center><b>该文件名已经存在,请您修改上传文件的名字.</b></center>";
request.setAttribute("msg", msg);
return mapping.findForward(forward);
}
String txt_path = BatchUtils.getFilePath();
BatchUtils batchUtils = new BatchUtils();
String path = batchUtils.getFilePath();
// txt_path += "updatefiles/";
// path += "updatefiles/";
File txt_dir = new File(txt_path);
File dir = new File(path);
if (!(dir.exists() && txt_dir.exists())) {
dir.mkdir();
txt_dir.mkdir();
}
String txt_out_file_name = txt_path + BatchUtils.Generate_File_Name()
+ ".TXT";
String out_file_name = path + "/" + BatchUtils.Generate_File_Name() // 文件写入的路径.
+ "." + file_postfix;
FileOutputStream txt_fos = new FileOutputStream(new File(
txt_out_file_name));
FileOutputStream fos = new FileOutputStream(new File(out_file_name));// 文件输出流. // //
// 写入到out_file_name.
PrintWriter txt_pw = new PrintWriter(txt_fos, true); // 通过现有的文件输出流进行输出.
// // 输出到txt_pw
PrintWr