您的位置:首页 > 编程语言 > Java开发

ajax和struts配合实现判断文件是否上传成功

2016-09-12 10:05 501 查看

背景:当用户将文件表单提交到后台的时候,系统需要判断是否提交完成,这个时候就需要利用插件集成的ajax实现此功能。

1、需要导入的jar包:

struts2的相关包(包括commons-fileupload-1.2.1.jar和commons-io-2.0.1.jar,还有相关的Jackson的jar包)

2、前台页面中的关键代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<!-- 导入jquery库 -->
<script src="../js/jquery-1.11.1.min.js"></script>
<!-- 这里需要使用一个form插件,因为当用户提交的不是简单的字符串而是大型数据,比如说是图片,视频等的时候,需要使用到该插件 -->
<script src="http://malsup.github.com/jquery.form.js"></script>
<script type="text/javascript">
$(function(){
$("#shan").click(function(){
var url="upload";
var args={"time":new Date() };console.log(args);
// 		ajax代码
$("#yuyu").ajaxSubmit({
type: "POST",
url:"upload",
dataType: "json",
success: function(data){
if(data!=null){

alert("您的信息已上传成功");
window.location="success.jsp";//跳转到相应的页面
}
else
alert("您的信息提交失败,请重新操作");
}
});

});
})

</script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form role="form" id="yuyu"  method="post" enctype="multipart/form-data">
<input type="file" id="showPicture" name="wj" />
<!--当使用到ajax进行表单提交的时候不要使用submit,因为会导致提交两次 -->
<button type="button" class="btn btn-primary" id="shan">上传</button>
</form>
</body>
</html>

3、后台action关键代码:

package com.xiaojie.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.opensymphony.xwork2.ActionSupport;

import Service.ProductService;

public class UploadAction extends ActionSupport implements RequestAware,ServletResponseAware {
/**
*
*/
private static final long serialVersionUID = 1L;
private HttpServletResponse response;
private ProductService productService=new ProductService();
private File wj;
private String wjContentType;//文件类型
private String wjFileName;//文件名
private Map<String, Object> request;
private String wjName;
public String getWjName() {
return wjName;
}
public void setWjName(String wjName) {
this.wjName = wjName;
}
private String wjDesc;

public File getWj() {
return wj;
}
public void setWj(File wj) {
this.wj = wj;
}
public String getWjContentType() {
return wjContentType;
}
public void setWjContentType(String wjContentType) {
this.wjContentType = wjContentType;
}
public String getWjFileName() {
return wjFileName;
}
public void setWjFileName(String wjFileName) {
this.wjFileName = wjFileName;
}
public String getWjDesc() {
return wjDesc;
}
public void setWjDesc(String wjDesc) {
this.wjDesc = wjDesc;
}
public void up() throws Exception {//切记处理ajax请求的时候,没有返回值!!!
System.out.println("action进来了");
// TODO Auto-generated method stub
//修改文件名
wjFileName=new Date().getTime()+wjFileName;
System.out.println(wj);
System.out.println(wjContentType);
System.out.println(wjFileName);
System.out.println(wjDesc);
System.out.println(wjName);
ServletContext servletContext=ServletActionContext.getServletContext();
String path=servletContext.getRealPath("/images/"+wjFileName);//文件最终要上传到的路径
System.out.println(path);
FileOutputStream out=new FileOutputStream(path);
FileInputStream in=new FileInputStream(wj);
byte[]buffer=new byte[1024];
int len=0;
while((len=in.read(buffer))!=-1){
out.write(buffer,0,len);

}
out.close();
in.close();
String name=wjName;
String desc=wjDesc;
String image="images/"+wjFileName;
request.put("image", image);
productService.save(name, desc, image);
ObjectMapper mapper=new ObjectMapper();
String result=mapper.writeValueAsString(wj);

//response.setContentType("text/javascript");
response.setCharacterEncoding("UTF-8");

response.getWriter().print(result);

//return null;
}
@Override
public void setRequest(Map<String, Object> arg0) {
// TODO Auto-generated method stub
this.request=arg0;
}
@Override
public void setServletResponse(HttpServletResponse arg0) {
// TODO Auto-generated method stub
this.response=arg0;
}
}<span style="color:#ff0000;">
</span>

注意:处理ajax请求的时候,没有返回值!!!

3、struts.xml配置

<pre name="code" class="html"><?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

<package name="default" namespace="/" extends="struts-default">
<action name="upload" class="com.xiaojie.action.UploadAction" method="up">
</action>

</package>

</struts>



如有疑问请联系本人qq:1913284695
或者微信:fyydbc
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: