Struts1.x 实现文件上传

在Struts1中,提供了较好的文件上传方法。
下面我通过一个简单的例子实现一个简单的文件上传功能:
A.通过MyEclipse新建一个java Web Project,命名为UpLoadWeb
B.为Web工程导入Struts1包(具体怎么操作可以GOOGLE)。
C.新建文件上传Action,UploadAction。
D.新建文件上传Form,UploadForm
E.将UploadAction和UploadForm配置到struts-config.xml中这样的话,整个工程的结构就已经搭建完成,接下来就是实现文件上传的功能了。

F.新建一个类命名为UploadFileClass,实现文件输出的功能。

package com.yourcompany.upload;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.struts.upload.FormFile;

public class UploadFileClass {
	// 方法用于上传文件到远程服务器上
	public UploadFileClass(FormFile formFile, String fileName,
			String toServerPath) throws Exception {
		if (formFile != null && toServerPath != null) {
			InputStream inputStream = formFile.getInputStream();
			OutputStream outputStream = new FileOutputStream(toServerPath + "/"
					+ fileName);
			int bytesRead = 0;
			byte[] bytesBuf = new byte[formFile.getFileSize()];

			while ((bytesRead = inputStream.read(bytesBuf)) != -1) {
				outputStream.write(bytesBuf, 0, bytesRead);
			}
			outputStream.write(bytesBuf);
			outputStream.close();
			inputStream.close();
		} else {
			throw new Exception("This error caused by some parameters is null!");
		}
	}
}


G.在UploadAction中添加代码如下,调用UploadFileClass中的上传方法:


package com.yourcompany.struts.action;


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;
import org.apache.struts.upload.FormFile;


import com.yourcompany.struts.form.UploadForm;
import com.yourcompany.upload.UploadFileClass;


public class UploadAction extends Action {
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		UploadForm uploadForm = (UploadForm) form;
		FormFile formFile = uploadForm.getUploadFile();
		try {
			String fileName = formFile.getFileName();
			System.out.println(fileName);
			String toServerPath = request.getRealPath("/");
			UploadFileClass uploadFileClass = new UploadFileClass(formFile,
					fileName, toServerPath);
			return mapping.findForward("success");
		} catch (Exception e) {
			e.printStackTrace();
			return mapping.findForward("failed");
		}
	}
}



H.为表单添加验证功能。


package com.yourcompany.struts.form;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

public class UploadForm extends ActionForm {
	private FormFile uploadFile;

	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();
		String fileName = uploadFile.getFileName();
		if (fileName == null || "".equals(fileName)) {
			errors.add("uploadFile", new ActionError("noUserFile"));
		}
		return errors;
	}

	public void reset(ActionMapping mapping, HttpServletRequest request) {
	}

	public FormFile getUploadFile() {
		return uploadFile;
	}

	public void setUploadFile(FormFile uploadFile) {
		this.uploadFile = uploadFile;
	}
}

I.主要页面upload.jsp


<%@ page language="java" pageEncoding="GBK"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>



	
		JSP for UploadForm form
	
	
		
			uploadFile : 
			
			

J.上传成功页面

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



	
		

		My JSP 'index.jsp' starting page
	

	
		文件上传成功!
	


K.上传失败页面

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    My JSP 'failed.jsp' starting page
  
  
  
  文件上传失败!
  


L.struts-config.xml配置信息





	
	
		

	
	
	
	
		
			
			
			
		
	
	


M.web.xml配置信息



	
	
		action
		org.apache.struts.action.ActionServlet
		
			config
			/WEB-INF/struts-config.xml
		
		
			debug
			3
		
		
			detail
			3
		
		0
	
	
		action
		*.do
	
	
		upload.jsp
	

================================OVER============================

工程源码请到地址:

http://download.csdn.net/detail/captaingan/4109944

下载

你可能感兴趣的:(struts,string,exception,html,path,myeclipse,Struts1.x)