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
PrintWriter pw = new PrintWriter(fos, true); // 输出流 输出到 pw
// StringBuffer s=new StringBuffer(batch_file);
StringReader s = new StringReader(batch_file);
// FileInputStream s1=new FileInputStream(batch_file);
BufferedReader br = new BufferedReader(s); // 字节流转向字符流. String line_str = null;
String s2 = null;
String s_ceed = null; // csv
String s_teed = null; // tsv
String s_xeed = null; // xls
int i = 0;
while ((line_str = br.readLine()) != null) {
if (file_postfix.equals("xls")) {
s_xeed = line_str.replaceAll(",", "|");
line_str = s_xeed;
txt_pw.println(line_str);
pw.println(line_str);// 写入文件到指定的地址.
} else if (file_postfix.equals("csv") && i > 0) {
s2 = line_str.replaceAll("\"", "");
s_ceed = s2.replaceAll(",", "|");
line_str = s_ceed;
txt_pw.println(line_str);
pw.println(line_str);// 写入文件到指定的地址.
} else if (file_postfix.equals("tsv") && i > 0) {
s_teed = line_str.replaceAll("\t", "|");
line_str = s_teed;
txt_pw.println(line_str);
pw.println(line_str);// 写入文件到指定的地址.

i++;
}
br.close();
txt_pw.close();
pw.close(); StaffCO staff = Login.getLoginInfo(request);
batchIn.setUpfilestaff(staff.getStaffID());
batchIn.setTransCode(BatchUtils.GET_BCARD_FILE);
batchIn.setFileName(txt_out_file_name);
batchIn.setOldfilename(file_name);
batchIn.setBcause(bcause);
batchIn.setNoteflag(che);
batchIn.setTextfield(textfield);
BatchOut batchOut = this.processFile(batchIn);
String msg = "";
if (batchOut == null) {
msg = "<center><b>上传批量充值文件失败</b></center>";
forward = "fail";
} else {
if (batchOut.getResult() != 4003)
msg = "<center><b>上传批量充值文件成功</b><br><b>文件号:"
+ batchOut.getFile_Id() + "</b></center>";
else {
msg = "<center><b>正在上传处理中,请稍后根据文件名称在大批量充值界面中查询上传文件信息</b></center>";
forward = "fail";
}
}
request.setAttribute("msg", msg);
return mapping.findForward(forward);
}
以上红色这句代码,如果上传的为xls文件,获取的值就是“ 邢唷��”开头的乱码,我试了各种转码都没办法。
另外网上的获取完整路劲名也获取不到。求大侠指教。

解决方案 »

  1.   

    你上传文件的代码不行,建议你去下载这个试试:http://download.csdn.net/detail/s478853630/4063293
      

  2.   

    你这2个问题,,先说获取文件路径也获取不到,,file_name = item.getName();  你先用数字或者字母试试看可以取到不!! 
    建议:fileupload一般这样做上传,,先取文件名保存到数据库!在用io流读写到服务器,在显示在浏览器上!
      

  3.   

    o ,是么? 我以为 直接充 form 中直接能得到那。。
      

  4.   

    为何不用common-io.jar里的FileUtils类里的方法呢!比你写的那个好用多了。