JAVAWeb SSH框架 上传文件,如2007的EXCEL
下面的代码是上传EXCEL的代码,其实,就是在上传文件到服务器,代码都差不多,只是接收的文件的类型改一下即可。
本人用的服务器是tomcat7,所以上传的文件保存在D:\MyWork2014\.metadata\.me_tcat7\webapps\Graduate\uploadExcel
1.jsp 用的是struts2 标签
在importExcel.jsp中,配置上传文件的按钮和提交的处理的action,最后还要在<s:form>标签里加上
enctype="multipart/form-data" 属性才行,要不然,action获取不到上传的文件。
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>上传文件</title> <link rel="stylesheet" type="text/css" href="styles.css"> </head> <body> <h4>导入Excel文件</h4> <s:form action="UploadExcel" name="ImportForm" enctype="multipart/form-data" method ="post"> <div><s:file name="upload"></s:file> <span><input type="submit" value="确定上传" style="background-color:#bbbbbb;color:white;width:90px;height:30px"/> </span> </div> </s:form> </body> </html>
2 .在struts.xml配置处理上传文件的action
</action> <action name="UploadExcel" class="uploadAction" method="uploadExcel"> <result name="uploadExcelSuccess">international.jsp</result> <result name="error">importExcel.jsp</result> <param name="allowedTypes">application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/octet-stream</param> <!-- //允许上传的文件类型,这个是2007EXCEL,即XLSX后缀 --> </action>
3.在applicationContext-action.xml文件中,实例化处理文件的uploadAction对象。
<bean id="uploadAction" class="com.international.action.uploadExcelAction" scope="prototype"> </bean>
4. 处理上传文件的uploadExcelaction.java的类
package com.international.action; import java.io.File; import org.apache.commons.io.FileUtils; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class uploadExcelAction extends ActionSupport{ private File upload; //用于接住jsp传过来的EXCEL文件 private String uploadFileName; //这个值不用进行处理,就能得到你传过来的EXCEL的文件名 private String uploadContentType; //uploadContentType这个值不用进行处理,就能得到你传过来的EXCEL文件的类型 //如: 如果是2007的EXCEL,就是application/vnd.openxmlformats-officedocument.spreadsheetml.sheet private String allowedTypes; //接住在struts2设置的值,用于进行文件类型验证 private String savePath; //设置绝对路径,用于存放上传的EXCEL public uploadExcelAction(){} public File getUpload() { return upload; } public void setUpload(File upload) { this.upload = upload; } public String getUploadFileName() { return uploadFileName; } public void setUploadFileName(String uploadFileName) { this.uploadFileName = uploadFileName; } public String getUploadContentType() { return uploadContentType; } public void setUploadContentType(String uploadContentType) { this.uploadContentType = uploadContentType; } public String getAllowedTypes() { return allowedTypes; } public void setAllowedTypes(String allowedTypes) { this.allowedTypes = allowedTypes; } public String getSavePath() { return savePath = ServletActionContext.getServletContext().getRealPath( "/uploadExcel"); } public void setSavePath(String savePath) { this.savePath = savePath; } public String uploadExcel() throws Exception { // 验证文件格式 // boolean flag = false; // System.out.println("uploadFileName: "+ uploadFileName + " uploadContentType: " + uploadContentType); // System.out.println("savePath:" + getSavePath() ); // String[] allowedTypesStr = allowedTypes.split(","); // for (int i = 0; i < allowedTypesStr.length; i++) { // if (uploadContentType.equals(allowedTypesStr[i])) { // flag = true; // } // } // if (flag == false) { // System.out.println("上传文件格式错误"); // return "error"; // } //保存文件 File newExcel = new File(getSavePath() + "\\" + uploadFileName); if (newExcel.exists()) { newExcel.delete(); } try { FileUtils.copyFile(upload, newExcel); } catch (Exception e) { e.printStackTrace(); } // 删除临时文件 upload.delete(); return "uploadExcelSuccess"; } }