加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
upload.jsp 2.06 KB
一键复制 编辑 原始数据 按行查看 历史
Vidhi Patel 提交于 2024-12-19 01:24 . Add files via upload
<%@ page import="java.io.*, java.util.*" %>
<%@ page import="javax.servlet.http.Part" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page isErrorPage="false" %>
<%@ page session="true" %>
<!DOCTYPE html>
<html>
<head>
<title>File Upload</title>
</head>
<body>
<h1>Upload a JSP File</h1>
<form action="upload.jsp" method="post" enctype="multipart/form-data">
<label for="file">Choose JSP File:</label>
<input type="file" name="file" id="file" accept=".jsp"><br><br>
<input type="submit" value="Upload File">
</form>
<%
try {
if ("POST".equalsIgnoreCase(request.getMethod())) {
// Enable multipart handling
Part filePart = request.getPart("file");
String fileName = filePart.getSubmittedFileName();
String uploadPath = application.getRealPath("/") + "uploads/";
// Create uploads directory if it doesn't exist
File uploadDir = new File(uploadPath);
if (!uploadDir.exists()) {
uploadDir.mkdirs();
}
// Save the uploaded file
try (InputStream inputStream = filePart.getInputStream();
FileOutputStream outputStream = new FileOutputStream(uploadPath + fileName)) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
out.println("<p>File uploaded successfully to: " + uploadPath + fileName + "</p>");
}
}
} catch (IllegalStateException e) {
out.println("<p>Error: Multipart configuration is missing or file size exceeds limits.</p>");
} catch (Exception e) {
out.println("<p>Error uploading file: " + e.getMessage() + "</p>");
}
%>
</body>
</html>
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化