编程语言
首页 > 编程语言> > 【汇智学堂】JAVAWEB开发日志

【汇智学堂】JAVAWEB开发日志

作者:互联网

jsp上传

<%@ page import="java.io.DataInputStream" %>
<%@ page import="java.io.File" %>
<%@ page import="java.io.FileOutputStream" %>
<%@ page import="javax.servlet.jsp.tagext.TryCatchFinally" %><%--
  Created by IntelliJ IDEA.
  User: soft
  Date: 2019/5/18
  Time: 14:11
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<%
    int MAX_SIZE=102400*102400;
    String rootPath;
    DataInputStream in=null;
    FileOutputStream fileOut=null;
    String remoteAddr=request.getRemoteAddr();

    rootPath=request.getSession().getServletContext().getRealPath("/")+"upload/";
    out.println("<h3>上传文件保存目录为"+rootPath+"</h3>");
    String contentType=request.getContentType();
    contentType=new String(contentType.getBytes(),"utf-8");

    try {
        if (contentType.indexOf("multipart/form-data") >= 0) {
            in = new DataInputStream(request.getInputStream());
            int formDataLength = request.getContentLength();
            if (formDataLength > MAX_SIZE) {
                out.println("<p>上传的文件字节数不可以超过" + MAX_SIZE + "</p>");
                return;
            }
            byte dataBytes[]=new byte[formDataLength];
            int byteRead=0;
            int totalBytesRead=0;
            while (totalBytesRead<formDataLength){
                byteRead=in.read(dataBytes,totalBytesRead,formDataLength);
                totalBytesRead+=byteRead;
            }

            String file=new String(dataBytes,"utf-8");
            //String saveFile=file.substring(file.indexOf("filename=\"")+10);
            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,contentType.length());

            String fileName = rootPath + saveFile;

            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;
            int startPos = ((file.substring(0,pos)).getBytes()).length;

            int endPos = ((file.substring(0,boundaryLocation)).getBytes()).length;

            File checkFile = new File(fileName);

            if(checkFile.exists()) {
                out.println("<p>" + saveFile + "文件已经存在.</p>");
            }

            File fileDir = new File(rootPath);

            if(!fileDir.exists()) {
                fileDir.mkdirs();
            }

            fileOut = new FileOutputStream(fileName);

            fileOut.write(dataBytes,startPos,(endPos-startPos));
            fileOut.close();
            out.println("<p align='center'><font color=red size=5>"+saveFile + "文件上传成功.</font></p>");
        }

        else{
            String content = request.getContentType();
            out.println("<p>上传的数据类型不是multipart/form-data</p>");
        }
    }

    catch(Exception ex){
    }
%>

<a href="upload.jsp">继续上传文件</a>

</body>
</html>


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<h2><img src="../../images/about.jpg"/> 上传文件</h2>

<div>
    <form action="upload1.jsp" enctype="multipart/form-data" method="post">
        选择一文件
        <input type="file" name="fileupload" value="upload"/><br>
        <input type="submit" value="上传">
        <input type="reset" value="取消">
    </form>
</div>

</body>
</html>


标签:文件,contentType,JAVAWEB,fileDir,request,汇智,new,日志,上传
来源: https://blog.csdn.net/weixin_44711285/article/details/94049385