import com.opensymphony.xwork2.ActionSupport;
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;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
public class UploadImageAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private static final int BUFFER_SIZE = 16 * 1024 ;
private File myFile; //网页中file字段的name
private String contentType; //struts2中必须字段,主要是set方法
private String fileName; // struts2中必须字段,主要是set方法
private String imageFileName; //要上传的图片在服务器中的名称
private String imagePath; //保存服务器中的路径
private String pagePath; //页面中要引用的url
public File getMyFile() {
return myFile;
}
public void setMyFile(File myFile) {
this.myFile = myFile;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getImageFileName() {
return imageFileName;
}
public void setImageFileName(String imageFileName) {
this.imageFileName = imageFileName;
}
public String getImagePath() {
return imagePath;
}
public void setImagePath(String imagePath) {
this.imagePath = imagePath;
}
public String getPagePath() {
return pagePath;
}
public void setPagePath(String pagePath) {
this.pagePath = pagePath;
}
public static long getSerialVersionUID() {
return serialVersionUID;
}
public static int getBUFFER_SIZE() {
return BUFFER_SIZE;
}
private static void copy(File src, File dst) {
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) {
e.printStackTrace();
}
}
public String execute(){
imageFileName = new Date().getTime() + fileName.substring(fileName.lastIndexOf("\\")+2,fileName.length()); //此名称用当前时间与上传图片名组成
imagePath = ServletActionContext.getServletContext().getRealPath( "/images/Image" ) + "\\" + imageFileName;
System.out.println(imagePath);
pagePath = getWebPath()+"images/Image" + "/" + imageFileName; //页面引用位置
System.out.println(pagePath);
File imageFile = new File(imagePath);
copy(myFile, imageFile);
return SUCCESS;
}
private String getWebPath(){
HttpServletRequest request = ServletActionContext.getRequest ();
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
return basePath;
}
}
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<title>upload</title>
<script type="text/javascript" src="js/jquery-1.5.2.js"></script>
<script type="text/javascript" src="js/jquery.js"></script>
</head>
<body>
<s:form id="imagefile" action="uploadimage" namespace="/" method="POST" name="pos"
enctype="multipart/form-data" >
<table width="80%" border="1" cellspacing="0" cellpadding="0">
<tr>
<td width="20%" align="right">
<font color="red">*</font>上传图片文件
</td>
<td width="20%">
<s:file name="myFile" id="myFile"></s:file>
</td>
<td colspan="2" align="center">
<input type="button" οnclick="getFileName()" value="上传" />
</td>
</tr>
</table>
<input type="hidden" name="fileName" id="fname">
</s:form>
</body>
<script type="text/javascript">
function getFileName(){
var filesName=document.getElementById("myFile").value;
document.getElementById("fname").value=filesName;
document.forms[0].submit();
//$.post("/CKeditor/ajax/fileUploads.action",{},function(){
//if(null!=filesName && ""!=filesName){
window.returnValue=filesName;
//window.close();
//}
//},"json");
//alert($("#myFile").val());
}
</script>
</html>