首先是 file.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<body>
<form action="FileUploadAction" method="post" enctype="multipart/form-data">
选择文件<input type="file" name="file" /></br>
<input type="submit" value="上传文件" /></br>
</form>
</body>
</body>
</html>
这里面将指向了FileUploadAction
FileUploadAction.java
package com.caizhen.file;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadAction extends ActionSupport {
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;
}
private File file;
private String fileFileName;
private String fileContentType;
public String execute() throws Exception {
// 获取要保存文件夹的物理路径(绝对路径)
String uploadPath = ServletActionContext.getServletContext().getRealPath("/upload");
File file1 = new File(uploadPath);
//测试此抽象路径名表示的文件或目录是否存在。若不存在,创建此抽象路径名指定的目录,包括所有必需但不存在的父目录。
if (!file1.exists())
file.mkdirs();
try {
FileUtils.copyFile(file, new File(file1, fileFileName));
} catch (IOException e) {
e.printStackTrace();
}
return SUCCESS;
}
}
file是上传的文件
fileName是上传文件的文件名
fileCoontenType是上传文件的类型
获取需要保存文件的物理地址uploadPath
String uploadPath = ServletActionContext.getServletContext().getRealPath("/upload");
File file1 = new File(uploadPath);
//通过将给定路径名字符串转换成抽象路径名来创建一个新 File 实例。
public boolean exists() //判断此抽象路径名表示的文件或文件夹是否存在
copyFile简介
@param srcFilePath 源文件路径
@param destFilePath 目标文件路径
方法一:传入源文件路径和目标文件路径
public static boolean copyFile(String srcFilePath, String destFilePath) {
return copyFile(getFileByPath(srcFilePath), getFileByPath(destFilePath));
}
@param srcFile 源文件
@param destFile 目标文件
方法二:传入原文件和目标文件
public static boolean copyFile(File srcFile, File destFile) {
return copyOrMoveFile(srcFile, destFile, false);
}
new File(file1, fileFileName)
通过给定的父抽象路径名和子路径名字符串创建一个新的File实例
File(File parent, String child);
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="Mypackage" namespace="/" extends="struts-default">
<action name="FileUploadAction" class="com.caizhen.file.FileUploadAction">
<result name="success">result.jsp</result>
<result name="input"></result>
<interceptor-ref name="defaultStack">
<param name="fileUpload.maxmumSize">4194340</param>
<param name="fileUpload.allowedExtensions">.txt,.doc.jpg,.zip</param>
<param name="fileUpload.allowedTypes">text/plain,application/msword,image/jpeg</param>
</interceptor-ref>
</action>
</package>
</struts>
扫描二维码关注公众号,回复:
2866191 查看本文章