閱讀50 返回首頁    go 阿裏雲 go 技術社區[雲棲]


struts2文件上傳

1.創建項目,添加struts2的支持,然後:


代碼:FileUpload類完成文件上傳:

package com.myphome;


import java.io.File;
import java.util.UUID;


import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;


import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;


/**
 * struts2文件上傳處理類
 * */
public class FileUpload {

private File upload;//保存上傳的文件,必須和頁麵上傳“type="file""的name相同
private String uploadFileName;//保存上傳的文件名稱,必須為“name”+FileName
private String uploadContentType;//保存上傳的文件類型,必須為“name”+ContentType

//提供屬性的set、get方法
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 uploadFile() throws Exception{
//獲取文件上傳的類型,實現文件類型安全驗證
if(!(uploadContentType.equalsIgnoreCase("image/pjpeg")||uploadContentType.equalsIgnoreCase("image/gif")||uploadContentType.equalsIgnoreCase("image/x-png"))){
ActionContext.getContext().put("message", "文件格式不正確");
return Action.SUCCESS;
}
System.out.println(uploadContentType);
if(upload!=null){
//上傳文件在服務器上的保存位置
String strfolder=ServletActionContext.getServletContext().getRealPath("/images");
System.out.println(strfolder);
File folder=new File(strfolder);
if(folder.exists()==false){
folder.mkdir();
}
//更換上傳文件名
String ext=uploadFileName.substring(uploadFileName.lastIndexOf("."));
String newFileName=UUID.randomUUID().toString()+ext;
System.out.println(newFileName);
//設置目標對象
File dest=new File(folder, newFileName);
//把臨時文件拷貝到目標文件
FileUtils.copyFile(upload, dest);
//設置視圖提示內容
ActionContext.getContext().put("message", "文件上傳成功");
}
return Action.SUCCESS;
}
}

struts.xml配置:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "https://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<!-- 此屬性規定了struts2文件上傳的內容允許 的最大字節數 -->
<constant name="struts.multipart.maxSize" value="2097152"></constant>
<package name="load"  namespace="/struts" extends="struts-default" >
<action name="upload" method="uploadFile">
<result name="success" type="dispatcher">/index.jsp</result>
</action>
</package>
</struts>    

界麵:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>struts2文件上傳</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
  </head>
  <body>
  <s:if test="%{message==null}">
<span >文件格式必須為圖片,大小不大於2G</span>
  </s:if>
  <s:if test="%{message!=null}">
  ${message}
  </s:if>
    <form action="${pageContext.request.contextPath}/struts/upload" method="post" enctype="multipart/form-data" name="formup" >
    上傳文件:<input type="file" name="upload" >
    <input type="submit" name="submit" value="確認上傳">
    </form>
  </body>
</html>

最後更新:2017-04-03 22:15:27

  上一篇:go 使用jsp自定義標簽庫實現數據列表顯示模擬cms4j中的標簽庫效果
  下一篇:go POJ 2826 兩線段關係求麵積