国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

JAVAWeb SSH框架 上傳文件,如2007的EXCEL

2019-11-14 22:36:16
字體:
來源:轉載
供稿:網友
javaWeb SSH框架 上傳文件,如2007的Excel

下面的代碼是上傳EXCEL的代碼,其實,就是在上傳文件到服務器,代碼都差不多,只是接收的文件的類型改一下即可。

1.jsp 用的是struts2 標簽

代碼:

<s:file name="upload">

用form表單提交到Action

2.struts2 代碼:

<action name="UploadExcel" class="com.javaweb.action.UploadExcelAction" method="uploadExcel"> <result name="uploadExcelSuccess" > view/uploadExcelSuccess.jsp </result> <result name="error" > view/uploadExcelErr.jsp </result> <param name="allowedTypes">application/vnd.openxmlformats-officedocument.sPReadsheetml.sheet</param> //允許上傳的文件類型,這個是2007EXCEL,即XLSX后綴 </action>

3.Action代碼:

public class UploadExcelAction extends ActionSupport {

 private File upload;(get,set代碼省略,自動生成的代碼而已)//用于接住jsp傳過來的EXCEL文件 private String uploadFileName;(get,set代碼省略 ,自動生成的代碼而已)//這個值不用進行處理,就能得到你傳過來的EXCEL的文件名

 //uploadContentType這個值不用進行處理,就能得到你傳過來的EXCEL文件的類型

//如: 如果是2007的EXCEL,就是application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

 private String uploadContentType;(get,set代碼省略,自動生成的代碼而已)

private String allowedTypes;(get,set代碼省略,自動生成的代碼而已)//接住在struts2設置的值,用于進行文件類型驗證

 private String savePath; //設置絕對路徑,用于存放上傳的EXCEL,get方法代碼修改了,set 方法不變如下:

 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; String[] allowedTypesStr = allowedTypes.split(","); for (int i = 0; i < allowedTypesStr.length; i++) { if (uploadContentType.equals(allowedTypesStr[i])) { flag = true; } } if (flag == false) { return "error"; } File newExcel = new File(getSavePath() + "http://" + uploadFileName); if (newExcel.exists()) { newExcel.delete(); } try { FileUtils.copyFile(upload, newExcel); } catch (Exception e) { e.printStackTrace(); } // 刪除臨時文件 upload.delete(); return "uploadExcelSuccess"; }

}


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 定陶县| 新河县| 遂宁市| 安溪县| 棋牌| 陵川县| 星子县| 林州市| 盘山县| 栾川县| 徐汇区| 江西省| 兴和县| 宁南县| 明光市| 南乐县| 广水市| 年辖:市辖区| 蓬莱市| 乐至县| 平武县| 遵义市| 松滋市| 广丰县| 包头市| 南漳县| 东方市| 友谊县| 兴业县| 东辽县| 仲巴县| 阜南县| 连南| 定远县| 荆门市| 潍坊市| 青阳县| 红原县| 陆良县| 五家渠市| 镇安县|