一、单文件上传
实例:
表单应该注意三个点 form中的method="post"、enctype="multipart/form-data"、input中的type="file"
fileForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
struts.xml中配置文件
FileOperationAction.java
package com.lz.web.action;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class FileOperation extends ActionSupport{
private File myFile;
private String myFileContentType;
private String myFileFileName;
private String destPath;
public File getMyFile() {
return myFile;
}
public void setMyFile(File myFile) {
this.myFile = myFile;
}
public String getMyFileContentType() {
return myFileContentType;
}
public void setMyFileContentType(String myFileContentType) {
this.myFileContentType = myFileContentType;
}
public String getMyFileFileName() {
return myFileFileName;
}
public void setMyFileFileName(String myFileFileName) {
this.myFileFileName = myFileFileName;
}
public String upload(){
destPath= ServletActionContext.getServletContext().getRealPath("/files");//文件保存的路径
System.out.println("realpath: "+destPath);
System.out.println("Src filename: "+myFile);
System.out.println("Dest filename: "+myFileFileName);
try {
File destFile = new File(destPath, myFileFileName);
FileUtils.copyFile(myFile, destFile);//拷贝文件
} catch (IOException e) {
e.printStackTrace();
return "error";
}
return "upload";
}
public String prefile(){
return "prefile";
}
}
file.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
二、多文件上传
fileForm2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
struts.xml中配置
MultiFileUploadAction.java
package com.lz.web.action;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class MultiFileUploadAction extends ActionSupport{
private File\[\] image;
private String\[\] imageFileName;
private String\[\] imageContentType;
public File\[\] getImage() {
return image;
}
public void setImage(File\[\] image) {
this.image = image;
}
public String\[\] getImageFileName() {
return imageFileName;
}
public void setImageFileName(String\[\] imageFileName) {
this.imageFileName = imageFileName;
}
public String\[\] getImageContentType() {
return imageContentType;
}
public void setImageContentType(String\[\] imageContentType) {
this.imageContentType = imageContentType;
}
public String load(){
return "load";
}
public String upload(){
String realPath = ServletActionContext.getServletContext().getRealPath("/files");//指定文件保存的路径
System.out.println("realpath: "+realPath);
try {
if(image!=null){
File savedir = new File(realPath);
if(!savedir.getParentFile().exists())//判断文件目录是否存在
savedir.getParentFile().mkdirs();
for(int i=0; i<image.length; i++){
File saveFile = new File(realPath, imageFileName\[i\]);
FileUtils.copyFile(image\[i\], saveFile);//文件复制
}
}
} catch (IOException e) {
e.printStackTrace();
}
return "upload2";
}
}
file2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
手机扫一扫
移动阅读更方便
你可能感兴趣的文章