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

struts+json实现注解配置

2011-11-01 22:45 113 查看
import java.awt.Image;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import javax.imageio.ImageIO;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
@ParentPackage("json-default")
public class FileUploadAction extends ActionSupport
{
private File file;
private String fileFileName;
private String fileFileContentType;
private String message = "你已成功上传文件";
private Integer width = 0;
private Integer height = 0;

public File getFile()
{
return file;
}

public void setFile(File file)
{
this.file = file;
}

public String getFileFileName()
{
return fileFileName;
}

public void setFileFileName(String fileFileName)
{
this.fileFileName = fileFileName;
}

public String getFileFileContentType()
{
return fileFileContentType;
}

public void setFileFileContentType(String fileFileContentType)
{
this.fileFileContentType = fileFileContentType;
}

@JSON
public String getMessage()
{
return message;
}

public void setMessage(String message)
{
this.message = message;
}

@JSON
public Integer getWidth()
{
return width;
}

public void setWidth(Integer width)
{
this.width = width;
}

@JSON
public Integer getHeight()
{
return height;
}

public void setHeight(Integer height)
{
this.height = height;
}

@Action(value = "uploadPic", results = { @Result(name = "success", type = "json") }, params = { "contentType", "text/html" })

public String uploadPic()
{
String path = ServletActionContext.getServletContext().getRealPath("/upload");
try
{
File f = this.getFile();
if (this.getFileFileName().endsWith(".exe"))
{
message = "对不起,你上传的文件格式不允许!!!";
return INPUT;
}
FileInputStream inputStream = new FileInputStream(f);
FileOutputStream outputStream = new FileOutputStream(path + "/" + this.getFileFileName());
byte[] buf = new byte[1024];
int length = 0;
while ((length = inputStream.read(buf)) != -1)
{
outputStream.write(buf, 0, length);
}
inputStream.close();
outputStream.flush();
message = fileFileName;
File imageFile = new File(path + "/" + fileFileName);
Image image = ImageIO.read(imageFile);
width = image.getWidth(null);
height = image.getHeight(null);
}
catch (Exception e)
{
e.printStackTrace();
message = "对不起,文件上传失败了!!!!";
}
return SUCCESS;
}
}

import java.awt.Image;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import javax.imageio.ImageIO;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
@ParentPackage("json-default")
public class FileUploadAction extends ActionSupport
{
private File	file;
private String	fileFileName;
private String	fileFileContentType;
private String	message	= "你已成功上传文件";
private Integer	width	= 0;
private Integer	height	= 0;

public File getFile()
{
return file;
}

public void setFile(File file)
{
this.file = file;
}

public String getFileFileName()
{
return fileFileName;
}

public void setFileFileName(String fileFileName)
{
this.fileFileName = fileFileName;
}

public String getFileFileContentType()
{
return fileFileContentType;
}

public void setFileFileContentType(String fileFileContentType)
{
this.fileFileContentType = fileFileContentType;
}

@JSON
public String getMessage()
{
return message;
}

public void setMessage(String message)
{
this.message = message;
}

@JSON
public Integer getWidth()
{
return width;
}

public void setWidth(Integer width)
{
this.width = width;
}

@JSON
public Integer getHeight()
{
return height;
}

public void setHeight(Integer height)
{
this.height = height;
}

@Action(value = "uploadPic", results = { @Result(name = "success", type = "json") }, params = { "contentType", "text/html" })
public String uploadPic()
{
String path = ServletActionContext.getServletContext().getRealPath("/upload");
try
{
File f = this.getFile();
if (this.getFileFileName().endsWith(".exe"))
{
message = "对不起,你上传的文件格式不允许!!!";
return INPUT;
}
FileInputStream inputStream = new FileInputStream(f);
FileOutputStream outputStream = new FileOutputStream(path + "/" + this.getFileFileName());
byte[] buf = new byte[1024];
int length = 0;
while ((length = inputStream.read(buf)) != -1)
{
outputStream.write(buf, 0, length);
}
inputStream.close();
outputStream.flush();
message = fileFileName;
File imageFile = new File(path + "/" + fileFileName);
Image image = ImageIO.read(imageFile);
width = image.getWidth(null);
height = image.getHeight(null);
}
catch (Exception e)
{
e.printStackTrace();
message = "对不起,文件上传失败了!!!!";
}
return SUCCESS;
}
}


文件可以上传成功。

客户端代码:

<%@ 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>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Struts2 、jquery之ajaxfileupload异步上传插件</title>

<script type="text/javascript" src="js/jquery.js"></script>

<script type="text/javascript" src="js/ajaxfileupload.js"></script>

<script type="text/javascript">

function ajaxFileUpload()

{

$.ajaxFileUpload

(

{

url:'uploadPic.gp',//用于文件上传的服务器端请求地址

secureuri:false,//一般设置为false

fileElementId:'file',//文件上传空间的id属性 <input type="file" id="file" name="file" />

dataType: 'json',//返回值类型 一般设置为json

success: function (data, status) //服务器成功响应处理函数

{

//从服务器返回的json中取出message中的数据,其中message为在struts2中action中定义的成员变量

$("#pic").val(data.message);

$("#width").val(data.width);

$("#height").val(data.height);

if(typeof(data.error) != 'undefined')

{

if(data.error != '')

{

alert(data.error);

}

else

{

alert(data.message);

}

}

},

error: function (data, status, e)//服务器响应失败处理函数

{

alert(e);

}

}

)

return false;

}

</script>

</head>

<body>

<p align="center">Struts2 、jquery之ajaxfileupload异步上传插件</p>

<table align="center" border="1" cellpadding="0" cellspacing="0">

<tr>

<td>图片地址:</td>

<td>

<input type="text" name="pic" id="pic" />

<br/>

<input type="file" id="file" name="file" />

<input type="button" value="上传" onclick="return ajaxFileUpload();">

</td>

</tr>

<tr>

<td>图片宽度:</td>

<td>

<input type="text" name="width" id="width" />

</td>

</tr>

<tr>

<td>图片高度:</td>

<td>

<input type="text" name="height" id="height" />

</td>

</tr>

</table>

</body>

</html>

总是执行action以后,返回的下载框,郁闷呀,请帮忙啦!我用是Struts2.1.8.1,自带的插件,采用

配置文件的形式可以成功,也就是这样的可以成功

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">

<struts>

<package name="struts2" extends="json-default">

<action name="fileUploadAction" class="com.jackie.ajaxfile.action.AjaxFileUploadAction">

<result type="json" name="success">

<param name="contentType">

text/html

</param>

</result>

<result type="json" name="error">

<param name="contentType">

text/html

</param>

</result>

</action>

</package>

</struts>

注解的不能成功。

------------------------------------------------------------------------------------------------------------------

问题补充:

ymw3607 写道
出现下载文件对话框,原因是

JSON插件将HTTP响应(Response)的MIME类型设为“application

/json”

怎么解决呢?

------------------------------------------------------------------------------------------------------------------

问题补充:

ymw3607 写道
出现下载文件对话框,原因是

JSON插件将HTTP响应(Response)的MIME类型设为“application

/json”

params = { "contentType", "application /json" }

我早试验了,我这样设置,照样是下载框。郁闷!

------------------------------------------------------------------------------------------------------------------

问题补充:

angel243fly 写道
感觉这个问题是浏览器的问题。

好像IE显示服务器端返回的JSON数据时就提示下载框,FF和chrome没有这个问题

ff试验了,也是下载框

------------------------------------------------------------------------------------------------------------------

问题补充:

fengyie007 写道
results = {@Result(name = "success", type = "json",params = {"contentType", "text/html"})}

把results设置改成这个试试。

这个params是传给json result的。

你用xml配置时

<result type="json" name="success">

<param name="contentType">

text/html

</param>

</result>

这个参数就是传给json result的。但你用注解时未添加这个参数。

你添加的那个params是传给action用的。

你说改成这样的?

@Action(value = "uploadPic", results = { @Result(name = "success", type = "json", params = { "contentType", "text/html" }) })

我试验了,还是载框。郁闷!

采纳的答案

2011-08-22 fengyie007 (初级程序员)

results = {@Result(name = "success", type = "json",params = {"contentType", "text/html"})}

把results设置改成这个试试。

这个params是传给json result的。

你用xml配置时

<result type="json" name="success">

<param name="contentType">

text/html

</param>

</result>

这个参数就是传给json result的。但你用注解时未添加这个参数。

你添加的那个params是传给action用的。

提问者对于答案的评价:

谢谢!

额外加分:5

问题答案可能在这里 →
寻找更多解答

ext2.0在提交form表单时上传文件的问题
[原创]Struts2 + JQuery + JSON实现AJAX
struts2 + jquery + json 进行ajax请求(转载及补充)
jquery-easy-ui 结合 struts2 jsonplugin convention 配置 错误。
struts2.1.8 ,使用自带json插件struts2-json-plugin-2.1.8.1.客户端得不到json对象,弹出下载action提示框

其他回答

感觉这个问题是浏览器的问题。

好像IE显示服务器端返回的JSON数据时就提示下载框,FF和chrome没有这个问题
angel243fly (初级程序员) 2011-08-21

出现下载文件对话框,原因是

JSON插件将HTTP响应(Response)的MIME类型设为“application

/json”
ymw3607 (初级程序员) 2011-08-22

引用
总是执行action以后,返回的下载框,郁闷呀,请帮忙啦!

你用什么浏览器执行的?用firebug看看返回的响应是什么样子的.
myali88 (资深架构师) 2011-08-22

你这个上传调用是用同步方式调用的吧。

所以返回内容是直接由浏览器解析,浏览器会把application /json类型当作下载文件。浏览器只能直接显示html代码,所以你把参数设置成这个试试。

params = { "contentType", "text/html" } 。

异步方式调用才应该设置为application /json吧。
fengyie007 (初级程序员) 2011-08-22

不是设置params = { "contentType", "text/html" } 哦。

是返回信息应该不能设置为JSON类型吧。
fengyie007 (初级程序员) 2011-08-22

我按作者的意思模拟了一下:

Java代码







@Action(
value = "say" ,
results = {
@Result( name = "success" , type="json" , params = { "contentType", "text/html" })

}
)
public String say() {
height = 20;

return "success";
}

@Action(
value = "say" ,
results = {
@Result( name = "success" , type="json" , params = { "contentType", "text/html" })
}
)
public String say() {
height = 20;

return "success";
}


没有加

Java代码







params = { "contentType", "text/html" }

params = { "contentType", "text/html" }
前,只有Chrome可以正常返回,FF和IE都返回下载框。而加上后,三种浏览器都返回正常。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: