引言
學習了框架之後,就要進行小例子的應用了……下面小編就敲了一個文件上傳的例子,還請大家指教!
主要步驟
1、創建用於文件上傳的jsp相關頁面
1)index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
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>My JSP 'index.jsp' starting page</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>
<li>測試strust文件上傳</li>
<form action="upload.do" method="post" enctype="multipart/form-data">
標題:<input type="text" name="title"><br>
文件:<input type="file" name="myfile"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
2)upload_success.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
文件上傳成功!<br>
標題=【${uploadForm.title }】<br>
文件名稱=【${uploadForm.myfile.fileName }】<br>
</body>
</html>
2、創建ActionForm
UploadActionForm
package com.bjpowernode.struts;
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
@SuppressWarnings("serial")
public class UploadActionForm extends ActionForm {
private String title;
//上傳的文件必須採用FormFile聲明
private FormFile myfile;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public FormFile getMyfile() {
return myfile;
}
public void setMyfile(FormFile myfile) {
this.myfile = myfile;
}
}
3、創建Action
UploadTestAction
package com.bjpowernode.struts;
import java.io.FileOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
/**
* 上傳Action
* @author Administrator
*
*/
public class UploadTestAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
UploadActionForm uaf = (UploadActionForm)form;
System.out.println("title=" + uaf.getTitle());
System.out.println("fileName=" + uaf.getMyfile().getFileName());
FileOutputStream fos = new FileOutputStream("c:\\" + uaf.getMyfile().getFileName());
fos.write(uaf.getMyfile().getFileData());
fos.flush();
fos.close();
return mapping.findForward("success");
}
}
4、配置
struts-config.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
<form-bean name="uploadForm" type="com.bjpowernode.struts.UploadActionForm"/>
</form-beans>
<action-mappings>
<action path="/upload"
type="com.bjpowernode.struts.UploadTestAction"
name="uploadForm"
scope="request"
>
<forward name="success" path="/upload_success.jsp"/>
</action>
</action-mappings>
<controller maxFileSize="10M"/>
</struts-config>
效果
總結
1、在Struts中,一個FormFile類型的對象對應Form表單中傳送的一個文件,Struts將上傳的文件信息封裝進FormFile中,通過FormFile提供的方法可以方便的進行文件的操作。其實FormFile是一個接口,位於 org.apache.struts.upload.FormFile 中,它定義了操作上傳文件的基本方法。
FormFile接口定義的常用方法:
(1) getFileName()/setFileName() //用於獲取或設置文件名;
(2) getFileSize() / setFileSize() //用於獲取或設置文件字節數;
(3) getFileData() //用於獲取文件的字節數組,用於小的文件;
(4) getInputStream() //用於獲取文件的輸入流,用於較大的文件;
(5) destory() //銷燬FromFile;
2、配置文件中限制文件大小
<controller maxFileSize="10M"/>
3、 注意:操作struts-config.xml文件時應當特別注意各個節點間的順序,因爲Struts要求配置文件的各個元素順序有一定的要求,順序一旦被打亂,也就意味着web容器即將進入癱瘓狀態,因此在添加<controler>節點時,要將此節點添加在<action-mapping>和<message-resources>節點之間。
上傳文件Demo已經做完了,期待小編的下次分享吧~~~~