将文件放在表单中与其他数据一起提交,这是最简单的文件上传实现。在做测试或者简单实验时可以使用这个方法来实现文件上传。
我把demo放在附件了,需要的可以下载附件,把它导进eclipse(Eclipse Java EE IDE for Web Developers),还缺少jar包,可以将struts下的struts2-blank样例里的jar拷贝过来。
表单
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <!DOCTYPE>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>File Upload</title>
- </head>
- <body>
- <div>
- <div>
- <h3>表单提交的方式上传文件</h3>
- </div>
- <div>
- <form action="noramlFileLoadAction" enctype="multipart/form-data"
- method="post">
- <div>
- <input type="file" name="file" id="file_upload" />
- </div>
- <div>
- <input type="submit" value="提交" />
- </div>
- </form>
- </div>
- <div>
- <hr />
- </div>
- </div>
- </body>
- </html>
提交后跳转的页面
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%@ taglib prefix="s" uri="/struts-tags"%>
- <!DOCTYPE>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>File Upload Result</title>
- </head>
- <body>
- <div>
- <%
- String isSuccess = request.getParameter("isSuccess");
- if ("1".equals(isSuccess)) {
- out.print("上传成功");
- } else if ("0".equals(isSuccess)) {
- out.print("上传失败");
- } else {
- out.print("上传结果未知");
- }
- %>
- </div>
- </body>
- </html>
Struts2配置文件
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
- "http://struts.apache.org/dtds/struts-2.3.dtd">
- <struts>
- <constant name="struts.devMode" value="true" />
- <constant name="struts.i18n.encoding" value="UTF-8" />
- <constant name="struts.ui.theme" value="simple" />
- <constant name="struts.multipart.maxSize" value="100" />
- <constant name="struts.multipart.saveDir " value="c:/tmp" />
- <package name="fileUpload" extends="struts-default" namespace="/">
- <action name="noramlFileLoadAction" class="com.study.action.FileUploadAction"
- method="execute">
- <param name="filePath">/filetemp</param>
- <result name="success">/NomalFileUploadResult.jsp?isSuccess=1</result>
- <result name="input">/NomalFileUploadResult.jsp?isSuccess=0</result>
- </action>
- </package>
- </struts>
FileCopyUtil(文件复制的工具类)
- package com.study.util;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.io.OutputStream;
- /**
- * Filecopy is used to copy file.
- * @author zhbhun
- *
- */
- public class FileCopyUtil {
- public static final int BUFFER_SIZE = 16 * 1024;
- /**
- * copy src to dst.
- * @param src
- * @param dst
- * @throws Exception
- */
- public static void copy(File src, File dst) throws Exception {
- try {
- InputStream in = null;
- OutputStream out = null;
- try {
- in = new BufferedInputStream(new FileInputStream(src),
- BUFFER_SIZE);
- out = new BufferedOutputStream(new FileOutputStream(dst),
- BUFFER_SIZE);
- byte[] buffer = new byte[BUFFER_SIZE];
- while (in.read(buffer) > 0) {
- out.write(buffer);
- }
- } finally {
- if (null != in) {
- in.close();
- }
- if (null != out) {
- out.close();
- }
- }
- } catch (Exception e) {
- throw new Exception("File copy error.", e);
- }
- }
- }
Action
- package com.study.action;
- import java.io.File;
- import org.apache.struts2.ServletActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- import com.study.util.FileCopyUtil;
- public class FileUploadAction extends ActionSupport {
- protected File file;
- protected String fileFileName;
- protected String fileContentType;
- protected String filePath;
- protected void restoreFile() throws Exception {
- if (this.file != null) {
- File temp = new File(ServletActionContext.getServletContext()
- .getRealPath(this.filePath) + "/" + this.fileFileName);
- FileCopyUtil.copy(this.file, temp);
- }
- }
- @Override
- public String execute() throws Exception {
- this.restoreFile();
- return "success";
- }
- public File getFile() {
- return file;
- }
- public void setFile(File file) {
- this.file = file;
- }
- public String getFileFileName() {
- return fileFileName;
- }
- public void setFileFileName(String fileFileName) {
- this.fileFileName = fileFileName;
- }
- public String getFileContentType() {
- return fileContentType;
- }
- public void setFileContentType(String fileContentType) {
- this.fileContentType = fileContentType;
- }
- public String getFilePath() {
- return filePath;
- }
- public void setFilePath(String filePath) {
- this.filePath = filePath;
- }
- }
备注:本文不涉及struts2文件上传的详细配置。