bbs/insert2.jsp
페이지 정보
![profile_image](https://dancepkt.cafe24.com/data/member_image/ad/admin.gif?1630310007)
본문
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.io.File" %>
<%@ page import="java.io.IOException" %>
<%@ page import="jakarta.servlet.ServletException" %>
<%@ page import="jakarta.servlet.annotation.MultipartConfig" %>
<%@ page import="jakarta.servlet.http.Part" %>
<%
// 업로드 폴더 설정
String uploadPath = application.getRealPath("") + File.separator + "uploads";
File uploadDir = new File(uploadPath);
if (!uploadDir.exists()) {
uploadDir.mkdir(); // 디렉토리가 없으면 생성
}
try {
// JSP에서 파일 업로드 처리
for (Part part : request.getParts()) {
String fileName = null;
// 파일 이름 추출
String contentDisp = part.getHeader("content-disposition");
for (String content : contentDisp.split(";")) {
if (content.trim().startsWith("filename")) {
fileName = content.substring(content.indexOf("=") + 2, content.length() - 1);
fileName = fileName.substring(fileName.lastIndexOf(File.separator) + 1); // 파일 경로 제외
}
}
// 파일 저장
if (fileName != null && !fileName.isEmpty()) {
String filePath = uploadPath + File.separator + fileName;
part.write(filePath);
out.println("File uploaded to: " + filePath + "<br>");
}
}
} catch (IOException | ServletException e) {
out.println("Error: " + e.getMessage());
}
%>
- 이전글bbs/insert.jsp 23.03.14
- 다음글bbs/view.jsp 23.03.14
댓글목록
등록된 댓글이 없습니다.