日期:2014-05-18 浏览次数:20654 次
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head> <title>文件上传</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
<form action="${pageContext.request.contextPath}/upload2/upload2.do" enctype="multipart/form-data" method="post">
文件:<input type="file" name="image">
<input type="submit" value="上传" />
</form>
<br/>
<s:fielderror />
</body>
</html>
package com.ljq.action;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class UploadAction extends ActionSupport {
private File image; // 上传的文件
private String imageFileName; // 文件名称
private String imageContentType; // 文件类型
public String execute() throws Exception {
String realpath = ServletActionContext.getServletContext().getRealPath(
"/excel");
// D:\apache-tomcat-6.0.18\webapps\struts2_upload\images
System.out.println("realpath: " + realpath);
if (image != null) {
File savefile = new File(new File(realpath), imageFileName);
if (!savefile.getParentFile().exists())
savefile.getParentFile().mkdirs();
FileUtils.copyFile(image, savefile);
ActionContext.getContext().put("message", "文件上传成功");
}
return "success";
}
/**
* @return the image
*/
public File getImage() {
return image;
}
/**
* @param image the image to set
*/
public void setImage(File image) {
this.image = image;
}
/**
* @return the imageFileName
*/
public String getImageFileName() {
return imageFileName;
}
/**
* @param imageFileName the imageFileName to set
*/
public void setImageFileName(String imageFileName) {
this.imageFileName = imageFileName;
}