注册 登录
编程论坛 J2EE论坛

javaweb 前台"文件上传"提交后服务器跳转后发生错误

deagoal 发布于 2022-11-17 15:19, 2780 次点击
javaweb 前台"文件上传"提交后服务器跳转后发生错误

###### javaweb 中, html页面input type=file  直接点出"上传"或选择一个小文件再点出"上传"时, 会因为没登录而成功转到别的页面. 选中一个2M以上的文件点上传, 服务器如果转发或是发送redriect时, 都会执行多次, 比如 ,下图展示的结果, filter拦截到2到3次. 前台浏览器等待一段时间后, 得到的结果是 ERR_CONNECTION_RESET
######
HTML页面:
<form action="<%=request.getContextPath() %>/s/FileUpload.yc" method="post" enctype="multipart/form-data">
请选择一个文件: <input type="file" name="upload" > <br><br>
<input type="submit" value="上传"/>
</form>


SERVLET
程序代码:
public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        //req.setCharacterEncoding("UTF-8");
System.out.println("uploadservlet doGet excecuting...");
        if(req.getSession().getAttribute("login") != null && (boolean)req.getSession().getAttribute("login")) {//登录
;
        }else {//没登录
System.out.println("uploadservlet doGet forward showinfo.jsp...");
                req.setAttribute("result", "没登录不能上传文件!!!");
                req.setAttribute("forward", "/login.jsp");
                req.getServletContext().getRequestDispatcher("/showinfo.jsp").forward(req, resp);return;
        }
        


###### CONSOLE台输出:
Filter ServletCharacterEncoding: /ftpWeb/s/FileUpload.yc
uploadservlet doGet excecuting
uploadservlet doGet forward showinfo.jsp
Filter ServletCharacterEncoding: /ftpWeb/s/FileUpload.yc
uploadservlet doGet excecuting
uploadservlet doGet forward showinfo.jsp
Filter ServletCharacterEncoding: /ftpWeb/s/FileUpload.yc
uploadservlet doGet excecuting
uploadservlet doGet forward showinfo.jsp

###### 如果没有这个forward或是redirect的话, 是可以正常上传的. 从结果看, 就象是用户提交了几次. 想过识别"重复提交"的解决. 想过把req的请求参数清除. 但没弄明白

###### 我想要达到的结果是 未登录时能正常转到登录页面. 而不是得到一个连接重置的结果.
2 回复
#2
deagoal2022-11-18 17:04
事实上, 不同浏览器得到的结果不同.
chrome有3个请求,我也不知道是不是浏览器发送的:
Filter ServletCharacterEncoding: /ftpWeb/s/FileUpload.yc
uploadservlet doGet excecuting... content length: 4457955542
uploadservlet doGet forward showinfo.jsp... DispatcherType: REQUEST
Filter ServletCharacterEncoding: /ftpWeb/s/FileUpload.yc
uploadservlet doGet excecuting... content length: 4457955542
uploadservlet doGet forward showinfo.jsp... DispatcherType: REQUEST
Filter ServletCharacterEncoding: /ftpWeb/s/FileUpload.yc
uploadservlet doGet excecuting... content length: 4457955542
uploadservlet doGet forward showinfo.jsp... DispatcherType: REQUEST
火狐有2个运行记录.
IE10有一个运行记录.
但最终结果无一不是浏览器得到连接中断的消息.
-----------------------------------------------
以下代码中,取得输入流, 并skip或read可以正常转向, 但是这两个方法都要真正读取全部数据(虽然最后都丢弃了), 很长时间后才会正常返回跳转页面. 所以还是不知道怎么直接丢弃请求数据并返回跳转
程序代码:
        }else {//没登录
System.out.println("uploadservlet doGet forward showinfo.jsp... DispatcherType: " + req.getDispatcherType());
            //if(req.getSession().getAttribute("toLogin") == null) {
               
//req.getSession().setAttribute("toLogin", "");
               
//resp.sendRedirect("/showinfo.jsp");return;
                ServletInputStream sis = req.getInputStream();
                sis.skip(req.getContentLengthLong());
                sis.close();
                req.setAttribute("result", "没登录不能上传文件!!!");
                 req.setAttribute("forward", "/login.jsp");
                req.getServletContext().getRequestDispatcher("/showinfo.jsp").forward(req, resp);return;
            //}
        }
#3
deagoal2022-11-18 17:28
req.setAttribute(ans.nextElement(), null); 好象可以清除数据------------------
一黑挫样    , 慢得要死.  看来只能用登录后才能选择文件来解决了.

程序代码:
System.out.println("uploadservlet doGet forward showinfo.jsp... DispatcherType: " + req.getDispatcherType());
            //if(req.getSession().getAttribute("toLogin") == null) {
               
//req.getSession().setAttribute("toLogin", "");
               
//resp.sendRedirect("/showinfo.jsp");return;
Enumeration<String> ans = req.getParameterNames();
for(;ans.hasMoreElements();) {
    String an = ans.nextElement();
    System.out.println(an + ":" + req.getParameter(an));
    req.setAttribute(ans.nextElement(), null);
}


                //ServletInputStream sis = req.getInputStream();
            
//    sis.skip(req.getContentLengthLong());
                req.setAttribute("result", "没登录不能上传文件!!!");
                 req.setAttribute("forward", "/login.jsp");
                req.getServletContext().getRequestDispatcher("/showinfo.jsp").forward(req, resp);return;
            //}


[此贴子已经被作者于2022-11-18 17:33编辑过]

1