ACTION代码
package ul;
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.io.PrintWriter;
import java.util.Date;
import org.apache.struts2.ServletActionContext;
import ccme.sys.base.BaseAction;
import ccme.sys.json.JSONUtil;
public class FileUploadAction extends BaseAction
{
private static final long serialVersionUID = 572146812454l ;
private static final int BUFFER_SIZE = 5 * 1024 ;
private File myFile;
private String contentType;
private String fileName;
private String imageFileName;
private String caption;
String newfilename;
String groupid;
private static void copy(File src, File dst) {
try {
InputStream in = null ;
OutputStream out = null ;
try {
FileOutputStream f=new FileOutputStream(dst);
in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE);
out = new BufferedOutputStream( f, 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();
}
}
private static String getExtention(String fileName) {
int pos = fileName.lastIndexOf(".");
return fileName.substring(pos);
}
@Override
public String execute()
{
PrintWriter out = null;
FilePojo filePojo=new FilePojo();
try {
this.getResponse().setContentType("text/html;charset=utf-8");
out = this.getResponse().getWriter();
imageFileName = new Date().getTime() + getExtention(fileName);
File imageFile = new File(ServletActionContext.getServletContext().getRealPath("/UploadImages")+"/"+imageFileName);
copy(myFile, imageFile);
filePojo.setFilename(imageFileName);
filePojo.setGroupid(this.getGroupid());
filePojo.setFileszie(imageFile.length());
filePojo.setResult("true");
}
catch (Exception e)
{
e.printStackTrace();
filePojo.setResult("error");
}
out.print(JSONUtil.bean2json(filePojo));
out.close();
return null;
}
public void setMyFileContentType(String contentType) {
this .contentType = contentType;
}
public void setMyFileFileName(String fileName) {
this .fileName = fileName;
}
public void setMyFile(File myFile) {
this .myFile = myFile;
}
public String getImageFileName() {
return imageFileName;
}
public String getCaption() {
return caption;
}
public void setCaption(String caption) {
this .caption = caption;
}
public String getNewfilename() {
return newfilename;
}
public void setNewfilename(String newfilename) {
this.newfilename = newfilename;
}
public String getGroupid() {
return groupid;
}
public void setGroupid(String groupid) {
this.groupid = groupid;
}
}
struts2配置文件
JSP页面
需要:jquery.min.js,ajaxfileupload.js
var formNum=0;
function addFile()
{
formNum = formNum + 1;
var file="";
file=file+"文件:";
file=file+"
";
file=file+"";
$("#mydiv").append(file);
}
function dlefile(obj)
{
$(obj).parent().remove();
}
function uploadFile()
{
var filelist=$(":file");
for(var i=0;i
ajaxFileUpload(filelist[i]);
}
function ajaxFileUpload(myFile)
{
var groupid=myFile.id.split("_")[1];
$.ajaxFileUpload
(
{
url:'fileUpload.action',
secureuri:false,
fileElementId:myFile.id,
dataType : 'json',
data: {"newfilename": "ccc123","groupid": groupid},
success: function (data, status)
{
$("#span_"+groupid).html("上传状态:"+data.result+"|服务器文件名称:"+data.filename+"|文件大小:"+data.fileszie+"
");//remove();
},
error: function (data, status, e)
{
alert("error");
}
}
);
return false;
}
文章转载自:http://blog.sina.com.cn/s/blog_81e93ca90100yqdd.html