Extjs + struts2 上传文件
昨天一直在弄文件上传,之前没有太明白是文件上传是怎么回事,今天试验成功了,也有点了解了。也就是将本地文件通过http文件写入到服务器,通俗的讲就要上传就是直接文件将文件从一个位置拷贝一份到另外一个地方,而另外一个地方指的就是在服务器里面的一个文件里面。那就只要先获得目标文件的路径,wap下面的存文件的文件夹路径。然后再通过流输入到指定文件里面。
js 代码
Ext.onReady(function() {
var fileForm = new Ext.form.FormPanel({
title : “”,
renderTo : “fileUpload”,
fileUpload : true,
layout : “form”,
id : “fileUploadForm”,
items : [{
id : 'upload',
name : 'upload',
fieldLabel : "选择",
inputType : "file",
xtype : "field"
}],
buttons : [{
text : "提交",
name : "submit",
handler : submit
}]
});
});
function submit() {
Ext.getCmp(“fileUploadForm”).getForm().submit({
url : “uploadAction.action”,
method : “POST”,
success : function(form, action) {
alert(“success”);
},
failure : function(form, action) {
alert(“failure”);
}
});
}
action代码
package com.cocobra.action;
import java.io.*;
import java.util.Date;
import java.util.UUID;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private File upload;
private String uploadContentType;
//这里要注意一点,uploadFileName 中的 upload 要与传入过来的File名字一样,不然获取不到文件的名字,今天就因为这里不一致,找了一上午没找到原因
private String uploadFileName;
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
// 上传文件的文件名
// private String getFileExp(String name) {
// int pos = name.lastIndexOf(“.”);
//
// return name.substring(pos);
// }
private static final int BUFFER_SIZE = 16 * 1024;
public String execute() throws Exception{
Date d = new Date();
System.out.println(“uploadFileName = “+this.uploadFileName); //要上传的文件名
//获得wapps的路径,uploader 指的放在文件的地方
String toSrc = ServletActionContext.getServletContext().getRealPath(“uploader”)+”/”+uploadFileName;
System.out.println(“toFile= “+toSrc);
File toFile = new File(toSrc);
writeFile(this.upload,toFile);
return SUCCESS;
}
private static void writeFile(File src, File dst) {
System.out.println(” == 文件寫入 == “);
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();
}
System.out.println(“写入成功!”);
}
}
//struts2.xml 配置文件一段
<action name=”uploadAction” >
<interceptor-ref name=”fileUpload”>
<!–拦截器strut2自带的, 指定上传文件的格式,如果不符合规定,将会自动拦截下来 –>
<param name=”allowedTypes”>image/bmp,image/png,image/gif,image/jpeg,image/jpg</param>
<param name=”maximumSize”>20000000000</param>
</interceptor-ref>
<interceptor-ref name=”defaultStack” />
<result name =”success” >/index.jsp</result >
</action>
本文来自:北漂一族[http://www.nanbianhz.com] 转载请注明详细出处:http://www.nanbianhz.com/extjs-struts2-update-fil.htm

最近评论