Struts1实现文件上传

引言

         学习了框架之后,就要进行小例子的应用了……下面小编就敲了一个文件上传的例子,还请大家指教!


主要步骤

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已经做完了,期待小编的下次分享吧~~~~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章