struts2生成图片验证码,页面获取是解析mimetype问题

jloveto 2014-01-16 01:40:30
struts2后台action生成图片验证码,前台登录部分调用,但是前台获取的时候出现解析格式问题 如下:
Resource interpreted as Image but transferred with MIME type text/html: "http://localhost:8080/SSHTest/common/createImage.do".
action配置如下:
<package name="ex" namespace="/common" extends="struts-default">
<action name="createImage" class="createImageAction">
<result name="success" type="stream">
<param name="contentType">image/jpg</param>
<param name="inputName">imageStream</param>
<param name="bufferSize">2048</param>
</result>
</action>
</package>

------------------------------------------------------------------------------
各位高手有知道的帮忙指点一二,感激不尽……
...全文
205 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
tony4geek 2014-01-16
  • 打赏
  • 举报
回复
public String intercept(ActionInvocation invocation) throws Exception { final Object action = invocation.getAction(); final ActionContext context = invocation.getInvocationContext(); … if (action instanceof SessionAware) { ((SessionAware) action)。setSession(context.getSession()); } … return invocation.invoke(); } 你这个 session.put("authcode", authcode);失败 是因为你的CreateImageAction  没实现 SessionAware 你实现看看
jloveto 2014-01-16
  • 打赏
  • 举报
回复
感谢大家的发言,@rui888,我是参考 你所给的http://blog.csdn.net/yao_qinwei/article/details/8244115这个链接里面的文章来操作的,经过我排查,居然居然是因为session引起的,具体还没弄明白,我使用ActionContext.getContext().getSession().put("authcode", authcode);将验证码放入session的时候页面可以正常,但是如果使用 session.put("authcode", authcode);的时候就会出现我所反映的问题…… 应该是这篇博客里面session经过spring DI注入的,而我没有做先关配置造成的
package com.ccnu.action;

import java.io.ByteArrayInputStream;
import java.util.Map;

import com.ccnu.utils.ImageUtils;
import com.ccnu.utils.RandomNumUtil;
import com.ccnu.utils.SysLogUtils;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class CreateImageAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 212375542572427219L;

	// 图片流
	private ByteArrayInputStream inputStream;

	// session域
	private Map<String, Object> session;

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public void setInputStream(ByteArrayInputStream inputStream) {
		this.inputStream = inputStream;
	}

	public ByteArrayInputStream getInputStream() {
		return inputStream;
	}

	@Override
	public String execute() throws Exception {
		// // 获取默认难度和长度的验证码
		String authcode = ImageUtils.getSecurityCode();
		SysLogUtils.info("系统生成验证码[" + authcode + "]");
		this.setInputStream(ImageUtils.getImageAsInputStream(authcode));
		 ActionContext.getContext().getSession().put("authcode", authcode);// 使用这个能正常运行
		// 取得随机字符串放入HttpSession
		//session.put("authcode", authcode);  //使用这个的时候会出现我反应的问题
		return SUCCESS;
	}

}
谢谢大家的热心……
tony4geek 2014-01-16
  • 打赏
  • 举报
回复
解决了分享下。
loveunittesting 2014-01-16
  • 打赏
  • 举报
回复
没怎么用过struts,都是使用response指定contentType的,你这个配置我感觉不是对应response的
tony4geek 2014-01-16
  • 打赏
  • 举报
回复
Content-Type:image/jpeg 应该是这样的。不过现在你不是这样的。 你点这个看下返回头信息 不知道你那里出问题。
jloveto 2014-01-16
  • 打赏
  • 举报
回复
引用 4 楼 rui888 的回复:
应该还是类型的问题。 0.9759968174621463: Response Headersview source Cache-Control:no-cache Content-Length:1919 Content-Type:image/jpeg;charset=UTF-8 Date:Thu, 16 Jan 2014 06:02:28 GMT Expires:Thu, 01 Jan 1970 00:00:00 GMT Pragma:No-cache Proxy-Connection:keep-alive Server:Apache-Coyote/1.1 X-Powered-By:Servlet 2.5; JBoss-5.0/JBossWeb-2.1 X-Via:1.1 zjyw13:9 (Cdn Cache Server V2.0)
你这个是做了模拟么? 你action配置怎样的?
tony4geek 2014-01-16
  • 打赏
  • 举报
回复
应该还是类型的问题。 0.9759968174621463: Response Headersview source Cache-Control:no-cache Content-Length:1919 Content-Type:image/jpeg;charset=UTF-8 Date:Thu, 16 Jan 2014 06:02:28 GMT Expires:Thu, 01 Jan 1970 00:00:00 GMT Pragma:No-cache Proxy-Connection:keep-alive Server:Apache-Coyote/1.1 X-Powered-By:Servlet 2.5; JBoss-5.0/JBossWeb-2.1 X-Via:1.1 zjyw13:9 (Cdn Cache Server V2.0)
jloveto 2014-01-16
  • 打赏
  • 举报
回复
引用 2 楼 rui888 的回复:
Content-type: image/jpeg")
这种 <param name="contentType">image/jpg</param> 写法难道有问题?
tony4geek 2014-01-16
  • 打赏
  • 举报
回复
Content-type: image/jpeg")
jloveto 2014-01-16
  • 打赏
  • 举报
回复
自己先顶一下,欢迎各位有经验的前辈来指导……

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧