struts1 上传文件

java代码:

package com.struts1.form;

import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;

public class UploadFileForm extends ActionForm {

	private static final long serialVersionUID = 1L;
	
	private FormFile formFile;

	public FormFile getFormFile() {
		return formFile;
	}

	public void setFormFile(FormFile formFile) {
		this.formFile = formFile;
	}
}

成员变量的顺序要和jsp页面上的输入框的顺序一致。


package com.struts1.action;

import java.io.FileOutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
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.struts1.form.UploadFileForm;

public class UploadAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		UploadFileForm uploadFileForm = (UploadFileForm)form;
		FormFile formFile = uploadFileForm.getFormFile();
		if (formFile == null) {
			return mapping.findForward("uploadError");
		}
		String formFileName = formFile.getFileName();
		int pos = formFileName.lastIndexOf(".");
		String fileName = "c:\\";
		if (pos != -1) {
			fileName = fileName + formFileName.substring(0, pos) + System.currentTimeMillis() + formFileName.substring(pos);
		} else {
			fileName = fileName + formFileName + System.currentTimeMillis();
		}
		FileOutputStream fos = null;
		
		try {
			fos = new FileOutputStream(fileName);
			IOUtils.copy(formFile.getInputStream(), fos);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			IOUtils.closeQuietly(formFile.getInputStream());
			IOUtils.closeQuietly(fos);
		}
		
		return mapping.findForward("success");
	}

}



jsp代码:

<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-nested" prefix="nested" %>
<!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=GBK">
<title>Insert title here</title>
</head>
<body>
<html:form action="uploadFile.do" enctype="multipart/form-data" method="post">
	<html:file property="formFile"></html:file><br>
	<html:submit />
</html:form>
</body>
</html>

注意:

enctype="multipart/form-data"

method="post"


struts配置文件:

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
          "http://struts.apache.org/dtds/struts-config_1_3.dtd">

<struts-config>

	<form-beans>
		<form-bean name="uploadFileForm" type="com.struts1.form.UploadFileForm" />
	</form-beans>

	<global-exceptions>
	</global-exceptions>

	<global-forwards>
	</global-forwards>

	<action-mappings>
		<action path="/uploadFile" type="com.struts1.action.UploadAction"
			name="uploadFileForm" scope="request" validate="false" input="/index.jsp">
			<forward name="success" path="/success.jsp" />
			<forward name="uploadError" path="/uploadError.jsp" />
		</action>
	</action-mappings>
	
	<controller maxFileSize="1M" />

</struts-config>

controller用来对上传的文件大小等进行控制,超过最大大小就抛异常。


你可能感兴趣的:(struts1 上传文件)