Struts2中,采用注解的方式配置json,总是出现下载框,郁闷之极!请帮忙!
[code="java"]
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;
}
}
[/code]
文件可以上传成功。
客户端代码:
<%@ 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">
Struts2 、jquery之ajaxfileupload异步上传插件
<br> function ajaxFileUpload()<br> {<br> $.ajaxFileUpload<br> (<br> {<br> url:'uploadPic.gp',//用于文件上传的服务器端请求地址<br> secureuri:false,//一般设置为false<br> fileElementId:'file',//文件上传空间的id属性 <input type="file" id="file" name="file" /><br> dataType: 'json',//返回值类型 一般设置为json<br> success: function (data, status) //服务器成功响应处理函数<br> {<br> //从服务器返回的json中取出message中的数据,其中message为在struts2中action中定义的成员变量<br> $("#pic").val(data.message);<br> $("#width").val(data.width);<br> $("#height").val(data.height);<br> if(typeof(data.error) != 'undefined')<br> {<br> if(data.error != '')<br> {<br> alert(data.error);<br> }<br> else<br> {<br> alert(data.message);<br> }<br> }<br> },<br> error: function (data, status, e)//服务器响应失败处理函数<br> {<br> alert(e);<br> }<br> }<br> )<br> return false;<br> }<br>
Struts2 、jquery之ajaxfileupload异步上传插件
图片地址: |
|
图片宽度: |
|
图片高度: |
|
总是执行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">
text/html
text/html
注解的不能成功。
results = {@Result(name = "success", type = "json",params = {"contentType", "text/html"})}
把results设置改成这个试试。
这个params是传给json result的。
你用xml配置时
text/html
这个参数就是传给json result的。但你用注解时未添加这个参数。
你添加的那个params是传给action用的。
感觉这个问题是浏览器的问题。
好像IE显示服务器端返回的JSON数据时就提示下载框,FF和chrome没有这个问题
出现下载文件对话框,原因是
JSON插件将HTTP响应(Response)的MIME类型设为“application
/json”
[quote]总是执行action以后,返回的下载框,郁闷呀,请帮忙啦![/quote]
你用什么浏览器执行的?用firebug看看返回的响应是什么样子的.
你这个上传调用是用同步方式调用的吧。
所以返回内容是直接由浏览器解析,浏览器会把application /json类型当作下载文件。浏览器只能直接显示html代码,所以你把参数设置成这个试试。
params = { "contentType", "text/html" } 。
异步方式调用才应该设置为application /json吧。
不是设置params = { "contentType", "text/html" } 哦。
是返回信息应该不能设置为JSON类型吧。
我按作者的意思模拟了一下:
[code="java"]
@Action(
value = "say" ,
results = {
@Result( name = "success" , type="json" , params = { "contentType", "text/html" })
}
)
public String say() {
height = 20;
return "success";
}
[/code]
没有加[code="java"]params = { "contentType", "text/html" }[/code]前,只有Chrome可以正常返回,FF和IE都返回下载框。而加上后,三种浏览器都返回正常。