struts 2.0 图片上传
来源:互联网 发布:linux下rime配置 编辑:程序博客网 时间:2024/05/22 16:15
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" onclick="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>
- struts 2.0 图片上传
- struts 图片上传 错误
- struts上传图片问题
- Struts实现图片上传
- Struts:实现图片上传
- Struts 多图片上传
- struts 上传图片
- struts 图片上传
- struts 图片上传 调试
- EXTJS+STRUTS图片上传
- struts图片上传
- Struts实现图片上传
- struts上传图片的类
- struts:实现图片的上传
- struts 中的图片文件上传
- 利用Struts进行图片上传
- xheditor+struts 实现图片上传
- 利用struts进行图片上传
- struts文件下载
- 浅谈字节序(Byte Order)及其相关操作(转自老赵)
- linux应用Xlib来显示内存图像数据到窗口的方式
- 设置eclipse console 显示条数
- Android游戏框架AndEngine使用入门
- struts 2.0 图片上传
- 十进制转二进制
- Android学习笔记--事件机制
- codec engine工程中使用ccs下编译的lib库
- js表单验证控制代码大全
- 新的起点
- spring配置收集-数据源
- 常用的正则表达式
- php编码转换函数介绍