Jackie_GP 2011-08-21 17:42
浏览 224
已采纳

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:&#39;uploadPic.gp&#39;,//用于文件上传的服务器端请求地址<br> secureuri:false,//一般设置为false<br> fileElementId:&#39;file&#39;,//文件上传空间的id属性 <input type="file" id="file" name="file" /><br> dataType: &#39;json&#39;,//返回值类型 一般设置为json<br> success: function (data, status) //服务器成功响应处理函数<br> {<br> //从服务器返回的json中取出message中的数据,其中message为在struts2中action中定义的成员变量<br> $(&quot;#pic&quot;).val(data.message);<br> $(&quot;#width&quot;).val(data.width);<br> $(&quot;#height&quot;).val(data.height);<br> if(typeof(data.error) != &#39;undefined&#39;)<br> {<br> if(data.error != &#39;&#39;)<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






注解的不能成功。

  • 写回答

7条回答

  • fengyie007 2011-08-22 13:29
    关注

    results = {@Result(name = "success", type = "json",params = {"contentType", "text/html"})}
    把results设置改成这个试试。
    这个params是传给json result的。

    你用xml配置时


    text/html


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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突