douzheyo2617 2013-11-21 21:50
浏览 599
已采纳

获取HTTP状态404 - 502.shtml请求的资源不可用。上传文件时

I'm getting the above error while using servlet I've written. the war file is set on Tomcat ver 7.0.39 installed on cPanel. the servlet compiled and tested on local machine no problem. I've learnet that there is a problem that has something to do with the cPanel/PHP config. I tried to play with the cPanel configuration but no luck I feel that it has nothing to do with the java code but I'll put the fileUploadServlet anyhow

EDIT: I was able to upload a very small-sized file so it has something to do with file size \ long procssing time

package servlet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;

import convertor.TextAnalayzer;

import exception.ZoharException;

import beans.ParashaBean;
import beans.UserBean;

import jdbcHandler.JDBCZhoarHandler;

import util.ParashaName;
import util.XmlUrelParaser;

@WebServlet(urlPatterns = { "/upload" }, loadOnStartup = 1)
@MultipartConfig
public class FileUploadServlet extends HttpServlet {

    private static final long serialVersionUID = 8626646959046203428L;
    private JDBCZhoarHandler appHandler = new JDBCZhoarHandler();
    public static final String ERROR_PARAMETER = "error";
    public static final String COMMAND_PARAMETER = "command";
    public static final String USER_ATTRIBUTE = "user";
    public static final String HANDLER_ATTRIBUTE = "handler";

    @Override
    public void init() throws ServletException {
        super.init();
        try {
            getServletConfig().getServletContext().setAttribute("list",
                    appHandler.viewParashot());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void service(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        String command = request.getParameter(COMMAND_PARAMETER);
        String nextPage = "/login.jsp";
        if ("convert".equals(command)) {
            nextPage = this.upload(request, response);
        } else if ("login".equals(command)) {
            nextPage = this.login(request, response);
        } else {
        }// do nothing!!

        this.getServletConfig().getServletContext()
                .getRequestDispatcher(nextPage).forward(request, response);
    }

    private String login(HttpServletRequest request,
            HttpServletResponse response) {
        String name = request.getParameter("userName");
        String password = request.getParameter("password");
        JDBCZhoarHandler handler = new JDBCZhoarHandler();
        try {
            UserBean user = handler.getUser(name, password);
            HttpSession session = request.getSession(true);
            session.setAttribute(HANDLER_ATTRIBUTE, handler);
            session.setAttribute(USER_ATTRIBUTE, user.getId());
            return "/uploadFile.jsp";
        } catch (Exception e) {
            request.setAttribute(ERROR_PARAMETER, e.getMessage());
            return "/login.jsp";
        }

    }

    private String upload(HttpServletRequest request,
            HttpServletResponse response) {

        // view artifacts
        HttpSession session = request.getSession(false);
        ParashaName parashaName = new ParashaName();
        JDBCZhoarHandler handler = (JDBCZhoarHandler) session
                .getAttribute(HANDLER_ATTRIBUTE);
        List<ParashaBean> list = null;
        try {
            list = handler.viewParashot();
        } catch (SQLException e1) {
            request.setAttribute(ERROR_PARAMETER, e1.getMessage());
        }
        session.setAttribute("list", list);

        // Processing file
        if ("convert".equals(request.getParameter("command"))) {

            OutputStream out = null;
            InputStream filecontent = null;

            try {
                // Create path components to save the file
                XmlUrelParaser xml = new XmlUrelParaser();
                SimpleDateFormat format = new SimpleDateFormat(
                        "dd-MM-yy_HH-mm-ss");
                final Part filePart = request.getPart("file");
                if (filePart.getSize() == 0) {
                    throw new ZoharException("you must upload a file first");
                }
                final String fileName = xml.getUR("incomingFilesDir")
                        + session.getAttribute(USER_ATTRIBUTE)
                        + parashaName.convert(Integer.parseInt(request
                                .getParameter("parasha")))
                        + format.format(new Date()) + ".docx";

                out = new FileOutputStream(new File(fileName));
                filecontent = filePart.getInputStream();

                int read = 0;
                final byte[] bytes = new byte[1024];

                while ((read = filecontent.read(bytes)) != -1) {
                    out.write(bytes, 0, read);
                }
                TextAnalayzer ta = new TextAnalayzer();
                Integer ID = (Integer)session.getAttribute("user");
                ta.analayze(fileName,
                        Integer.parseInt(request.getParameter("parasha")),
                        Boolean.parseBoolean(request.getParameter("orginal")),
                        ID);
                request.setAttribute(ERROR_PARAMETER, "Upload complete");
                return "/uploadFile.jsp";
            } catch (Exception e) {
                request.setAttribute(ERROR_PARAMETER, e.getMessage());
            } finally {
                try {
                    if (out != null) {
                        out.close();
                    }
                    if (filecontent != null) {
                        filecontent.close();
                    }
                } catch (IOException e) {
                    request.setAttribute(ERROR_PARAMETER, e.getMessage());
                }
            }
        }
        return "/login.jsp";
    }
} 
  • 写回答

1条回答 默认 最新

  • dongre6404 2013-12-13 06:41
    关注

    This is a resault of memory lack. Better memory-managing code solved the problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!