m0_59908602 2022-05-04 20:53 采纳率: 0%
浏览 223
已结题

关于#HTTP状态 500 - 内部服务器错误#的问题,如何解决?代码应该没问题 使用的是IDEA

<!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>Insert title here</title>
    <style type="text/css">
        body {
            background: #99FFCC;
        }

    </style>
</head>
<body>
<ul>
    <%
        //定义上载文件的最大字节
        int MAX_SIZE = 10240 * 102400;
// 创建根路径的保存变量
        String rootPath;
        String fileName="";
//声明文件读入类
        DataInputStream in;
        FileOutputStream fileOut;
//取得客户端的网络地址
        //获得服务器的名字
        String serverName = request.getServerName();
//取得互联网程序的绝对地址
        String realPath = request.getRealPath(serverName);
        realPath = realPath.substring(0,realPath.lastIndexOf("\\"));
//创建文件的保存目录
        rootPath = realPath + "\\upload\\";
//取得客户端上传的数据类型
        String contentType = request.getContentType();
        try{
            if(contentType.contains("multipart/form-data")){
//读入上传的数据
                in = new DataInputStream(request.getInputStream());
                int formDataLength = request.getContentLength();
                if(formDataLength > MAX_SIZE){
                    return;
                }
//保存上传文件的数据
                byte[] dataBytes = new byte[formDataLength];
                int byteRead;
                int totalBytesRead = 0;
//上传的数据保存在byte数组
                while(totalBytesRead < formDataLength){
                    byteRead = in.read(dataBytes,totalBytesRead,formDataLength);
                    totalBytesRead += byteRead;
                }
//根据byte数组创建字符串
                String file = new String(dataBytes);
//out.println(file);
//取得上传的数据的文件名
                String saveFile = file.substring(file.indexOf("filename=\"") + 10);
                saveFile = saveFile.substring(0,saveFile.indexOf("\n"));
                saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
                int lastIndex = contentType.lastIndexOf("=");
//取得数据的分隔字符串
                String boundary = contentType.substring(lastIndex + 1);
//创建保存路径的文件名
                fileName = rootPath + saveFile;
//out.print(fileName);
                int pos;
                pos = file.indexOf("filename=\"");
                pos = file.indexOf("\n",pos) + 1;
                pos = file.indexOf("\n",pos) + 1;
                pos = file.indexOf("\n",pos) + 1;
                int boundaryLocation = file.indexOf(boundary,pos) - 4;
//out.println(boundaryLocation);
//取得文件数据的开始的位置
                int startPos = ((file.substring(0,pos)).getBytes()).length;
//out.println(startPos);
//取得文件数据的结束的位置
                int endPos = ((file.substring(0,boundaryLocation)).getBytes()).length;
//out.println(endPos);
//检查上载文件是否存在
                File checkFile = new File(fileName);
                if(checkFile.exists()){
                }
//检查上载文件的目录是否存在
                File fileDir = new File(rootPath);
                if(!fileDir.exists()){
                    fileDir.mkdirs();
                }
//创建文件的写出类
                fileOut = new FileOutputStream(fileName);
//保存文件的数据
                fileOut.write(dataBytes,startPos,(endPos - startPos));
                fileOut.close();
            }
            else{
            }
        }
        catch(Exception ex){
            throw new ServletException(ex.getMessage());
        }

    %>


运行结果及报错内容

img

  • 写回答

16条回答 默认 最新

  • zcl_1991 2022-05-05 10:26
    关注

    都爆500了 还敢说代码没问题

    评论

报告相同问题?

问题事件

  • 系统已结题 5月12日
  • 赞助了问题酬金20元 5月4日
  • 赞助了问题酬金20元 5月4日
  • 修改了问题 5月4日
  • 展开全部

悬赏问题

  • ¥15 怎么获取下面的: glove_word2id.json和 glove_numpy.npy 这两个文件
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug