您的位置:首页 > 编程语言 > Java开发

文件上传下载——通过struts的FormFile上传单个excel文件

2011-06-22 10:58 351 查看
通过struts的FormFile上传单个excel文件

思路:

1、通过struts的FormFile获取File(这个文件的路径是“客户端的选择的路径地址”)

2、将客户端的文件,以流的形式,存放到服务器端指定的目录

3、读取服务器端的excel文件,先获取工作簿workbook,然后获取这个工作簿的sheet,然后获取sheet中的每一行

4、校验: (1)、将sheet中的数据,按行读取,并插入到临时表中

(2)、针对这次导入数据,按列校验——每一列一个select,查出不符合规则的id

(3)、如果校验不通过,针对同一个错误,返回一批id,通过request返回到页面

(4)、如果校验都通过,将临时表中的数据拷贝到正式表,清空临时表,将正式表的数据封装成对象返回

jsp页面:

<td colspan="2">
<html:file property="theFile" size="10" />
</td>
<td>
<a onClick="if(document.all('theFile').value!='') uploadAndCheck();"class="butlink">

<span class="but2"> 上传 </span>

</a>
</td>

function uploadAndCheck(){

orgStaffChangeForm.action=orgStaffChangeForm.action;
setMethodAndNoConfirm('uploadAndCheck');
}

后台方法:

public ActionForward uploadAndCheck(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)throws Exception {

OrgStaffChangeForm frm = (OrgStaffChangeForm)form;

//通过struts的FormFile获取文件
FormFile file = (FormFile)frm.getTheFile();
String fileName = (String)file.getFileName();
int fileSize = file.getFileSize();

//通过getInputStream()方法取得文件流
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
InputStream is = null;
OutputStream fos = null;
String filepath=null;

try {
//此处若没有upload目录,则新建此目录
java.io.File dir = new java.io.File(this.getServlet().getServletContext().getRealPath("/")+"upload//");
if(!dir.exists()){
dir.mkdir();
}

is = (InputStream) file.getInputStream();//把文件读入
bis = new BufferedInputStream(is);
filepath = this.getServlet().getServletContext().getRealPath("/")+"upload//"+fileName;//取当前系统路径
fos = new FileOutputStream(filepath);//建立一个上传文件的输出流
bos = new BufferedOutputStream(fos);

//文件最大限额
int fileMaxSize = 10 * 1024 * 1024;

if (fileSize > fileMaxSize) {
//文件大小不能超过fileMaxSize,如果超过,报"上传文件尺寸超过10M"错误;
throw new Exception("上传文件大小不能超过10M");
}

int bytesRead = 0;
byte[] buffer = new byte[5 * 1024];
while ((bytesRead = bis.read(buffer)) != -1) {
//将文件从客户端读入到服务器指定目录
bos.write(buffer, 0, bytesRead);
}
}catch (Exception e) {
//设置文件物理上传出现问题时的出现的错误信息
throw new Exception("上传文件时发生错误,请重试或与管理员联系!");
}finally {
if (bos != null) {
bos.close();
}
if (bis != null) {
bis.close();
}
}

Workbook workbook = Workbook.getWorkbook(new File(filepath));
Sheet sheet = workbook.getSheet(0);
int rowCount = sheet.getRows();

Map result = new HashMap();

if(rowCount < 21){
result.put("less", "导入数据不能小于20行!");
request.setAttribute("result", result);
return mapping.findForward(MAIN_FORM);
}else if(rowCount > 2000){
throw new Exception("导入数据不能大于2000行!");
}

List list = new ArrayList();
for (int k = 1;k<rowCount;k++){
Cell[] cellDetail = sheet.getRow(k);//通过cellDetail[i]可获得每一个单元格的数据
list.add(cellDetail);//list中存放的是excel中一行的数据
}

//将数据传到后台处理
IOrgStaffChangeManager boam = (IOrgStaffChangeManager) getBean("orgStaffChangeManager");
ArrayList resultList  = this.organizeCheckData(frm, request, response);
result = boam.dealUploadData(list,frm.getUploadId(),resultList);

frm.setEmpChangeUpload(true);
request.setAttribute("result", result);

//数据校验通过
if( result.get("success") !=null){
List listempUpload = new ArrayList();// 校验通过后,存放返回的对象的list
List collSalaryLists=(List)result.get("success");
HashSet orgEmpChanges = new HashSet();
Iterator iter=collSalaryLists.iterator();
while (iter.hasNext()){
OrgEmpChangeTemp cell = (OrgEmpChangeTemp) iter.next();
OrgEmpChange orgEmpChange = new OrgEmpChange();

orgEmpChange.setNewEmpId(cell.getNewEmpId());
.......
listempUpload.add(orgEmpChange);

}

}
frm.setUploadId(frm.getUploadId());

request.setAttribute("wls", wls);
request.setAttribute("listempUpload", listempUpload);
return mapping.findForward(MAIN_FORM);
}


注意事项:

1、FormFile是struts包对外的一个接口,而且org.apache.struts.upload包是使用的commons-fileupload-1.0进行的封装。

FormFile的实现依然使用commons-fileupload-1.0版本的DiskFileUpload类。

DiskFileUpload这个类,commons-fileupload已经弃用了,取而代之的是ServletFileUpload类了

2、如果使用了它来实现文件上传的功能,则必须是FormFile对象在被初始化以后才能使用,它在进入Action之前就已经初始化好了!

3、struts是默认使用org.apache.struts.upload.CommonsMultipartRequestHandler类来处理FormFile指定的上传文件的。

4、 Struts根本没有把上传过程中出的超出最大值的异常带到Action,因为那是不可能的,而是把它放到了rquest的Attribute里。

而出了其他异常如enctype不对,磁盘空间不足怎么办?很遗憾,Struts没有去处理它,而是log了一下,抛给了上一层了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: