import java.awt.Image;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import javax.imageio.ImageIO;
import net.fckeditor.handlers.PropertiesLoader;
import net.fckeditor.jackie.util.FileUtil;
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;@SuppressWarnings("serial")
@ParentPackage("json-default")
public class ImageUploadAction extends BaseAction
{
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;
}

public String getMessage()
{
return message;
}

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

public Integer getWidth()
{
return width;
}

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

public Integer getHeight()
{
return height;
}

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

// 跳转到添加广告页面
@Action(value = "toImageUpload", results = { @Result(name = "success", location = "/WEB-INF/banner/ImgFileUpload.jsp") })
public String toImageUpload()
{
return SUCCESS;
}

@Action(value = "imageUpload", results = { @Result(name = "success", type = "json") }, params = { "contentType", "text/html" })
public String imageUpload()
{
String path = ServletActionContext.getServletContext().getRealPath(PropertiesLoader.getUserFilesPath() + PropertiesLoader.getImageResourceTypePath()) + "/";
FileUtil.newFolder(path);
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;
}
}文件是可以上传了,但是返回值,总是提示下载,奇怪的很,请高人帮忙呀!

解决方案 »

  1.   


    我是采用注解的方式呀。@Action(value = "imageUpload", results = { @Result(name = "success", type = "json") }, params = { "contentType", "text/html" })
      

  2.   


    楼主是用的struts2的json插件吧,struts.xml中继承一下,然后修改一下<struts>
    <package name="struts2.1" extends="json-default">
    <result name="success" type="json"> 
    <param>.....</param>
    </result>
    </package>
    </struts>这里推荐楼主使用google的Gson工具
      

  3.   


    我是采用的注解的方式的,不用struts.xml的配置文件的呀。
      

  4.   

    这里推荐楼主使用google的Gson工具,也可以呀,有例子吗?
      

  5.   


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

  6.   

    看懂了,lz的写法,觉得没有问题啊。。试试outputStream.flush();后在加一个outputStream.close()吧!
      

  7.   

    1.你的JSON是这个插件帮你转的,很有可能是你的数据转成JSON的时候报错,不是什么数据都能转的,要满足一定的条件的,把源代码加进去自己跟踪肯定能发现问题,都是开源的,没有问题解决不了