作者:标榜贾_120 | 来源:互联网 | 2014-05-27 11:53
ajaxfileupload.js插件,就是先利用jQuery的选择器获得file文件上传框中的文件路径值,然后动态的创建一个iframe,并在里面建立一个新的file文件框,提供post方式提交到后台。最后,返回结果到前台。html!--引入相关的js文件,相对路径--scripttypet
ajaxfileupload.js插件,就是先利用jQuery的选择器获得file文件上传框中的文件路径值,然后动态的创建一个iframe,并在里面建立一个新的file
文件框,提供post方式提交到后台。最后,返回结果到前台。
服务器代码:
public class UpdateAction extends DispatchAction {
public ActionForward
uploader(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
UpFormForm upFormForm = (UpFormForm) form;
FormFile ff = upFormForm.getHouseMaps();
try {
InputStream is =
ff.getInputStream();
File file = new File("D:/" +
ff.getFileName());
//指定文件存储的路径和文件名
OutputStream os = new
FileOutputStream(file);
byte[] b = new byte[1024];
int len = 0;
while((len = is.read(b)) !=
-1){
os.write(b, 0, len);
}
os.close();
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
struts.xml配置文件中的配置方法:
text/html
text/html
上传处理的Action
ImageUploadAction.action
package com.test.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Arrays;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class ImageUploadAction extends ActionSupport {
private File imgfile;
private String imgfileFileName;
private String imgfileFileContentType;
private String message = "你已成功上传文件";
public File getImgfile() {
return imgfile;
}
public void setImgfile(File imgfile) {
this.imgfile = imgfile;
}
public String getImgfileFileName() {
return imgfileFileName;
}
public void setImgfileFileName(String
imgfileFileName) {
this.imgfileFileName = imgfileFileName;
}
public String getImgfileFileContentType() {
return imgfileFileContentType;
}
public void setImgfileFileContentType(String
imgfileFileContentType) {
this.imgfileFileCOntentType=
imgfileFileContentType;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@SuppressWarnings("deprecation")
public String execute() throws Exception {
String path =
ServletActionContext.getRequest().getRealPath("/upload/mri_img_upload");
String[] imgTypes = new String[] { "gif",
"jpg", "jpeg", "png","bmp" };
try {
File f = this.getImgfile();
String fileExt =
this.getImgfileFileName().substring(this.getImgfileFileName().lastIndexOf(".")
+ 1).toLowerCase();
if (!Arrays.
asList(imgTypes).contains(fileExt)) {
message="只能上传
gif,jpg,jpeg,png,bmp等格式的文件!";
return ERROR;
}
FileInputStream inputStream = new
FileInputStream(f);
FileOutputStream outputStream =
new FileOutputStream(path + "/"+ this.getImgfileFileName());
byte[] buf = new byte[1024];
int length = 0;
while ((length =
inputStream.read(buf)) != -1) {
outputStream.write(buf,
0, length);
}
inputStream.close();
outputStream.flush();
} catch (Exception e) {
e.printStackTrace();
message = "文件上传失败了!!!!";
}
return SUCCESS;
}
}