读书人

excel下传至服务器有关问题

发布时间: 2012-12-26 14:39:28 作者: rapoo

excel上传至服务器问题
package com.spring.action;

import java.io.FileInputStream;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.struts2.ServletActionContext;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;

import cn.ehoo.baopay.util.UploadFile;

import com.HibernateSessionFactory.wx.HibernateSessionFactory;
import com.lei.wx.MyConn;
import com.opensymphony.xwork2.ActionSupport;

public class LoadAction extends ActionSupport {
private static Session session2;
private POIFSFileSystem fs;
private HSSFWorkbook wb;
private HSSFSheet sheet;
private HSSFRow row;
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
private File[] myFile;
private String[] myFileContentType;
private String[] myFileFileName;

static {
Configuration fig = new Configuration().configure("hibernate.cfg.xml");
session2 = HibernateSessionFactory.getSession();
}

public String[] readExcelTitle(InputStream is) {
try {
fs = new POIFSFileSystem(is);
wb = new HSSFWorkbook(fs);
} catch (IOException e) {
e.printStackTrace();
}
sheet = wb.getSheetAt(0);
row = sheet.getRow(0);
// 标题总列数
int colNum = row.getPhysicalNumberOfCells();
String[] title = new String[colNum];
for (int i = 0; i < colNum; i++) {
title[i] = getStringCellValue(row.getCell((short) i));
}
return title;
}

public Map<Integer, String> readExcelContent(InputStream is) {
Map<Integer, String> content = new HashMap<Integer, String>();
String str = "";
try {
fs = new POIFSFileSystem(is);
wb = new HSSFWorkbook(fs);
} catch (IOException e) {
e.printStackTrace();
}
sheet = wb.getSheetAt(0);
// 得到总行数
int rowNum = sheet.getLastRowNum();
row = sheet.getRow(0);
int colNum = row.getPhysicalNumberOfCells();
// 正文内容应该从第二行开始,第一行为表头的标题
if (request.getParameter("checkbox") != null) {
if (request.getParameter("checkbox").equals("商业")) {
for (int i = 1; i <= rowNum; i++) {
row = sheet.getRow(i);
int j = 0;
while (j < colNum) {
// 每个单元格的数据内容用"-"分割开,以后需要时用String类的replace()方法还原数据


// 也可以将每个单元格的数据设置到一个javabean的属性中,此时需要新建一个javabean
str += getStringCellValue(row.getCell((short) j))
.trim()
+ "-";
j++;
}
content.put(i, str);
str = "";
}
}
} else if (request.getParameter("checkbox2").equals("聚财")) {
for (int i = 1; i <= rowNum; i++) {
row = sheet.getRow(i);
int j = 0;
while (j < colNum) {
// 每个单元格的数据内容用"-"分割开,以后需要时用String类的replace()方法还原数据
// 也可以将每个单元格的数据设置到一个javabean的属性中,此时需要新建一个javabean
str += getStringCellValue(row.getCell((short) j)).trim()
+ "-";
j++;
}
content.put(i, str);
str = "";
}
}
return content;
}

private String getStringCellValue(HSSFCell cell) {
String strCell = "";
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_STRING:
strCell = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
strCell = String.valueOf(cell.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
strCell = String.valueOf(cell.getBooleanCellValue());
break;
case HSSFCell.CELL_TYPE_BLANK:
strCell = "";
break;
default:
strCell = "";
break;
}
if (strCell.equals("") || strCell == null) {
return "";
}
if (cell == null) {
return "";
}
return strCell;
}

public String Load() {
try {
// 对读取Excel表格标题测试
//String myFile = request.getParameter("myFile");
// if (str.equals("")) {
// request.setAttribute("name", "请添加文件!");
// return "fail";
// }

List<Map<String, Object>> list = null;
String path = null;
String name = null;
System.out.println(myFile);
if (myFile != null) {
list = UploadFile.uploadFile(myFile, myFileFileName,myFileContentType, path, name);
if (list.size() > 0) {
Map map = list.get(0);
path = (String) map.get("path");
}
}
System.out.println("path===" + path);

LoadAction excelReader = new LoadAction();

InputStream is2 = new FileInputStream(path);
Map<Integer, String> map = excelReader.readExcelContent(is2);

if (request.getParameter("checkbox") != null) {
if (request.getParameter("checkbox").equals("商业")) {
for (int i = 1; i <= map.size(); i++) {
String[] arr = new String[4];
arr = (map.get(i)).split("-");
String time = request.getParameter("year") + "/"
+ request.getParameter("month");

try {
Statement stat = MyConn.getConnection()
.createStatement();
String sql = "insert into PAY.BUSINESS_PAY (ID,JOB_NUMBER,NAME,DEPARTMENT,POSITION,STANDARD,ATTENDANCE,SUBSIDY,SENIORITY,MEAL,WAGES,PENSION,UNEMPLOYMENT,MEDICAL,CPF,AMOUNT,RUBBISH,TAXABLE_INCOME,INDIVIDUAL_INCOME_TAX,TAKE_HOME_PAY,EXIST_REST,MONTH) values (1,'"


+ arr[0]
+ "','"
+ arr[1]
+ "','"
+ arr[2]
+ "','"
+ arr[3]
+ "','"
+ arr[4]
+ "','"
+ arr[5]
+ "','"
+ arr[6]
+ "','"
+ arr[7]
+ "','"
+ arr[8]
+ "','"
+ arr[9]
+ "','"
+ arr[10]
+ "','"
+ arr[11]
+ "','"
+ arr[12]
+ "','"
+ arr[13]
+ "','"
+ arr[14]
+ "','"
+ arr[15]
+ "','"
+ arr[16]
+ "','"
+ arr[17]
+ "','"
+ arr[18]
+ "','"
+ arr[19]
+ "','"
+ time
+ "')";
stat.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
request.setAttribute("name", "上传成功!");
request.setAttribute("str", path);
return "success";
} else if (request.getParameter("checkbox2").equals("聚财")) {
for (int i = 1; i <= map.size(); i++) {
String[] arr = new String[4];
arr = (map.get(i)).split("-");
String time = request.getParameter("year") + "/"
+ request.getParameter("month");
try {
Statement stat = MyConn.getConnection()
.createStatement();
String sql = "insert into PAY.WEALTH_PAY (ID,NAME,DEPARTMENT,POSITION,POST_SALARY,SHOULD_ATTENDANCE,ACTUAL_ATTENDANCE,DEDUCTION,MEMBER_DEDUCTION,LATE,OTHER,DUTY,SUBSIDY,MEAL,OVERTIME,SENIORITY,WAGES_PAYABLE,PENSION,MEDICAL,UNEMPLOYMENT,RUBBISH,TAXABLE_INCOME,TAX,TAKE_HOME_PAY,EXIST_REST,MONTH) values (1,'"
+ arr[0]
+ "','"
+ arr[1]
+ "','"
+ arr[2]
+ "','"
+ arr[3]
+ "','"
+ arr[4]
+ "','"
+ arr[5]
+ "','"
+ arr[6]
+ "','"
+ arr[7]
+ "','"
+ arr[8]
+ "','"
+ arr[9]
+ "','"
+ arr[10]
+ "','"
+ arr[11]
+ "','"
+ arr[12]
+ "','"
+ arr[13]
+ "'"
+ ",'"
+ arr[14]
+ "','"
+ arr[15]
+ "','"
+ arr[16]
+ "','"
+ arr[17]
+ "','"
+ arr[18]
+ "','"
+ arr[19]
+ "','"
+ arr[20]
+ "','"
+ arr[21]
+ "','"
+ arr[22]
+ "','"
+ arr[23]
+ "','"
+ time
+ "')";
stat.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();


}
}
}
request.setAttribute("name", "上传成功!");
request.setAttribute("str", path);
return "success";
} catch (FileNotFoundException e) {
e.printStackTrace();
request.setAttribute("name", "未找到指定路径的文件!");
return "fail";

}
}
}
我的向服务器上传,找不到路径 这部分有问题
List<Map<String, Object>> list = null;
String path = null;
String name = null;
System.out.println(myFile);
if (myFile != null) {
list = UploadFile.uploadFile(myFile, myFileFileName,myFileContentType, path, name);
if (list.size() > 0) {
Map map = list.get(0);
path = (String) map.get("path");
}
}
System.out.println("path===" + path);

[解决办法]
你也至少说下出什么问题了啊
[解决办法]
我现在能上传excel文件,但不能获取表单中的name值,也就是request.getParameter()不能用了,
SmartUpload su = new SmartUpload();//新建一个SmartUpload对象
su.getRequest().getParameter( );取单个参数单个值
我现在用这个也获取的是null,我该怎么办啊??

读书人网 >Java Web开发

热点推荐