使用jsp实现文件上传的功能

 2023-09-19 阅读 18 评论 0

摘要:首先是表单的jsp文件:upload.jsp <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>文件上传</title> </head> <body> <h1>文件上传</h1

首先是表单的jsp文件:upload.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>文件上传</title>
</head>
<body>
<h1>文件上传</h1><form action="message.jsp" method="post" enctype="multipart/form-data">选择一个文件:<input type="file" name="uploadFile"/><br/><br/><input type="submit" value="上传"/>
</form>
</body>
</html>

  上传处理的jsp文件:message.jsp:

<%--Created by IntelliJ IDEA.User: DELLDate: 2018/4/20Time: 14:10To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.io.*,java.util.*,javax.servlet.*,javax.servlet.http.*" %>
<%@ page import="java.rmi.ServerException" %>
<html>
<head><title></title>
</head>
<body>
<%//定义上传文件的最大字节int MAX_SIZE = 102400 * 102400;//创建根路径的保存变量String rootPath;//声明文件读入类DataInputStream in = null;FileOutputStream fileOut = null;//取得互联网程序的绝对地址String realPath = request.getSession().getServletContext().getRealPath("/");realPath = realPath.substring(0, realPath.indexOf("\\out"));
//    out.print(realPath);//创建文件的保存目录rootPath = realPath + "\\web\\upload\\";//取得客户端上传的数据类型String contentType = request.getContentType();try {if (contentType.indexOf("multipart/form-data") >= 0) {//读入上传数据in = new DataInputStream(request.getInputStream());int formDataLength = request.getContentLength();if (formDataLength > MAX_SIZE) {out.print("上传的字节不可以超过" + MAX_SIZE + "字节");return;}//保存上传文件的数据byte dataBytes[] = new byte[formDataLength];int byteRead = 0;int totalBytesRead = 0;//上传的数据保存在byte数组里面while (totalBytesRead < formDataLength) {byteRead = in.read(dataBytes, totalBytesRead, formDataLength);totalBytesRead += byteRead;}//根据byte数组创建字符串String file = new String(dataBytes, "utf-8");//取得上传数据的文件名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>");return;}//检查上传文件的目录是否存在File fileDir = new File(rootPath);if (!fileDir.exists()) {fileDir.mkdirs();}//创建文件的输出类fileOut = new FileOutputStream(fileName);//保存文件的数据fileOut.write(dataBytes, startPos, (endPos - startPos));fileOut.close();out.print("<b>文件上传成功</b>");} else {String content = request.getContentType();out.print("上传的文件类型是" + content + "类型的,请上传目录mutipart/form-data类型的文件");}} catch (Exception ex) {throw new ServerException(ex.getMessage());}
%>
</body>
</html>

  

 

转载于:https://www.cnblogs.com/mmykdbc/p/8917317.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/78819.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息