文章目录

源码

index.jsp

<%--
  Created by IntelliJ IDEA.
  User: Atlantis
  Date: 2019-04-01
  Time: 21:23
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>index</title>
  </head>
  <body>
  <p>选择要上传的文件:</p><br>
  <form action="accept.jsp" method="post" enctype="multipart/form-data">
    <input type="file" name="boy" size="38">
    <br><input type="submit" value="提交" name="g">
  </form>
  </body>
</html>

accept.jsp

<%@ page import="java.io.IOException" %><%--
  Created by IntelliJ IDEA.
  User: Atlantis
  Date: 2019-04-01
  Time: 21:34
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import = "java.io.*" %>
<html>
<head>
    <title>accept</title>

</head>
<body>
<%
    try{
        InputStream inputStream = request.getInputStream();
        /*创建文件的目录*/
        File dir = new File("D:/workSpace/IDEAWorkSpace/projectOfFile/jsp_of_file_up_ex1");
        dir.mkdir();
        /*创建文件*/
        File file = new File(dir,"B.txt");
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        byte b[] = new byte[1000];
        int n ;
        while ((n = inputStream.read(b))!=-1){
            fileOutputStream.write(b, 0,n);
        }
        fileOutputStream.close();
        inputStream.close();
        out.print("文件已上传");
    }catch (IOException e){
        out.print("上传失败,错误信息:"+e.toString());
    }
%>
</body>
</html>

运行结果

index
JSP练习之文件上传
accept_success
JSP练习之文件上传

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-06-13
  • 2022-12-23
  • 2022-12-23
  • 2021-11-24
  • 2021-10-28
猜你喜欢
  • 2021-11-08
  • 2021-06-17
  • 2021-07-05
  • 2022-01-01
  • 2021-09-10
  • 2022-12-23
相关资源
相似解决方案