jsp页面提交后在跳回同一页面,中文全是????怎么回事

scoredhigh 2016-07-19 09:40:35
jsp页面提交后在跳回同一页面,中文全是????的乱码,怎么回事?
页面的编码设置<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>这句已经加上
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
以上这些都已经在代码里加上,仍然是中文全是????,这个和一般的乱码好像不一样。一般的乱码只要设置了上面的语句都能解决。这种全是????的乱码只在一种情况下出现,即页面提交后跳回同一页面。页面里的form的method是post,我试过,如果在这个页面用超链接的方式向action请求,再跳回同一页面,就不会有全是???乱码的问题。也就是说,提交form时才会有全是???乱码的问题,求答案。。。
...全文
238 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
lz123a 2017-12-12
  • 打赏
  • 举报
回复
楼主怎么解决的,我也碰到中文都是?号的问题,求解答。。。
scoredhigh 2016-07-19
  • 打赏
  • 举报
回复
项目里有很多页面都没有这种情况,唯独这个页面出现,我仔细分析了下,这个页面与其他页面唯一不同的地方在于form里有上传文件。 如果我点击“默认封面”那个按钮,通过超链接在转回这个页面,就不会出现全是??问号的乱码,这个超链接就不会上传文件。所以我大概认为出现问题的原因应该是这里。
scoredhigh 2016-07-19
  • 打赏
  • 举报
回复
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
//response.setContentType("text/html;charset=UTF-8");
//request.setCharacterEncoding("UTF-8");
String uid = (String)session.getAttribute("uid");
String nickname = (String)session.getAttribute("nickname");
String bookid = request.getParameter("bookid");
String cover = request.getParameter("cover");

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>封面管理</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	

<style type="text/css">
	<!--

	-->
	</style>
 
  </head>
  
  <body>
      
				<form action="/ECSystem/cover?bookid=<%=bookid %>" method="post" enctype="multipart/form-data">
				  <label>
				  <input type="file" name="file">
				  </label>
				  <label>
				  <input type="submit" name="Submit" value="上传封面"><input type="button" name="Submit2" onClick="window.location.href='/ECSystem/cover?bookid=<%=bookid %>&defaultcover=true'" value="默认封面">
				  </label>
				  <br>
			  </form>
			  <label>
				  <input type="image" name="imageField" width="145px" height="210" src="<%=cover %>">
			  </label>	
		
  
  
  </body>
</html>

大雨将至 2016-07-19
  • 打赏
  • 举报
回复
你的意思是说get请求不会乱码,post请求乱码? 理论上不会出这种问题 你把你的代码贴全一些看看
scoredhigh 2016-07-19
  • 打赏
  • 举报
回复
引用 3 楼 autfish 的回复:
是否是存入数据库,再取出后乱码了呢
这个都没有问题。存入数据库,取出都不会有乱码,这些乱码主要是页面上的中文字显示乱码。进入cover.jsp页面是中文是不会乱码的,只有当提交action,然后转回到cover.jsp才会全是???乱码。如果是在cover.jsp里用超链接向action发请求处理,再转回到cover.jsp,就不会乱码。也就是说,只在form里提交之后,才产生的乱码。
大雨将至 2016-07-19
  • 打赏
  • 举报
回复
是否是存入数据库,再取出后乱码了呢
scoredhigh 2016-07-19
  • 打赏
  • 举报
回复
引用 1 楼 qq_26508409 的回复:
就是重新跳转回来变了?看看跳转方法设置的什么?
调转设置就是在struts.xml里: <action name="cover" class="com.struts.action.CoverManagerAction"> <result>/cover.jsp</result> </action> 这个cover.jsp就是那个提交的页面。在action里处理玩了再跳回cover.jsp。
nikyotensai 2016-07-19
  • 打赏
  • 举报
回复
就是重新跳转回来变了?看看跳转方法设置的什么?
scoredhigh 2016-07-19
  • 打赏
  • 举报
回复
谢谢各位的回答,找到原因了,是因为在copy函数里有这样的语句:document_out.print("<script>parent.callback('upload file success');</script>"); 导致了输出中文全是问号,猜想原因也是因为没有设定编码格式,再次感谢罗
scoredhigh 2016-07-19
  • 打赏
  • 举报
回复
引用 9 楼 qq_32574091 的回复:
会不会就是你那个jsp文件的字符集编码与代码设置的不一样啊
右键jsp文件 属性
qq_32574091 2016-07-19
  • 打赏
  • 举报
回复
会不会就是你那个jsp文件的字符集编码与代码设置的不一样啊 右键jsp文件 属性
scoredhigh 2016-07-19
  • 打赏
  • 举报
回复
定位到引起这个乱码的地方了,但是不知道原因,在我的action处理类中,如果注释掉将上传的图片copy到服务器的代码,就不会出现乱码。
package com.struts.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.service.IBookinfoService;
import com.userinfo.Bookinfo;

public class CoverManageAction {
	private String fileFileName;
	private File file;
	private static final int BUFFER_SIZE = 64 * 1024 ; //缓冲器大小
	private String imageFileName;
	private String bookid;
	private IBookinfoService bookinfoService;
	private String cover;
	private String defaultcover = "false";
	
	public void setDefaultcover(String defaultcover) {   
        this.defaultcover = defaultcover;   
    }
	
	public String getCover() {   
        return this.cover;   
    }
	
	public String getBookid() {   
        return this.bookid;   
    }
	
	public void setBookid(String bookid) {   
        this.bookid = bookid;   
    }
	
	public void setFile(File file) {   
        this.file = file;   
    }
	
	public void setBookinfoService(IBookinfoService bookinfoService) {
		this.bookinfoService = bookinfoService;
	}
	
	public void setFileFileName(String fileFileName) {   
        this.fileFileName = fileFileName;   
    }
	
	public int getFileSizes(File f) throws Exception {//取得文件大小
		int s = 0;
		if (f.exists()) {
			FileInputStream fis = null;
			fis = new FileInputStream(f);
			s= fis.available();
		if (fis != null)
			fis.close();
		}
		return s;
	}
	
	private void copy(File src, File dst) throws Exception {
		if (!src.exists())
			return;
		PrintWriter document_out = ServletActionContext.getResponse().getWriter();
		Boolean result = true;
		int filesize = getFileSizes(src);
		try {
			InputStream in = null ;
			OutputStream out = null ;
			try { 
				in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE);
				out = new BufferedOutputStream(new FileOutputStream(dst), BUFFER_SIZE);
				byte [] buffer = new byte [BUFFER_SIZE];
				while (in.read(buffer, 0, filesize) > 0 ) {
					out.write(buffer, 0, filesize);
				}
			} finally {
				if ( null != in) {
					in.close();
				} 
				if ( null != out) {
					out.close();
				} 
			} 
		} catch (Exception e) {
			result = false;
			e.printStackTrace();
		} 
		if (result == true) {
			document_out.print("<script>parent.callback('upload file success');</script>");
		}
		else {
			document_out.print("<script>parent.callback('upload file fail');</script>");
		}
	} 
	
	private static String getExtention(String fileName) {
		int pos = fileName.lastIndexOf( "." );
		return fileName.substring(pos);
	}
	
	public String execute() throws Exception {
		if (this.defaultcover.equals("true")) {
			this.imageFileName = "./img/cover/cover.jpg";
		}
		else {
			/*
			String filename = "./img/cover/" + this.bookid + "_cover";
			this.imageFileName =  filename + getExtention(this.fileFileName);
			File imageFile = new File(ServletActionContext.getServletContext().getRealPath("/") + imageFileName);
			copy(file, imageFile);
			*/
			this.imageFileName = this.fileFileName;
		}
		//ActionContext.getContext().
		//ActionContext cxt = ActionContext.getContext();
		//HttpServletRequest request = (HttpServletRequest)cxt.get(ServletActionContext.HTTP_REQUEST);
		//request.setCharacterEncoding("utf-8");
		
		Bookinfo bookinfo;
		bookinfo = this.bookinfoService.getBookinfoByBookid(Long.parseLong(this.bookid));
		if (bookinfo != null) {
			bookinfo.setCover(imageFileName);
			this.bookinfoService.UpdateBookinfo(bookinfo);
			this.cover = imageFileName;
			ActionContext.getContext().getSession().put("bookid", this.bookid);
			ActionContext.getContext().getSession().put("cover", this.cover);
		}
		
		return "success";
	}
}
上面的代码我注释了 /* String filename = "./img/cover/" + this.bookid + "_cover"; this.imageFileName = filename + getExtention(this.fileFileName); File imageFile = new File(ServletActionContext.getServletContext().getRealPath("/") + imageFileName); copy(file, imageFile); */ 以后,cover.jsp就不会出现乱码的问题,但是注释的代码是一段写图片文件的功能,为什么和页面有关系,实在搞不懂!!!

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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