PartUploadPro1Servlet.java
페이지 정보
본문
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
@WebServlet("/partUploadPro1")
@MultipartConfig(
fileSizeThreshold=0,
location = "C:\\upload"
)
public class PartUploadPro1Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public PartUploadPro1Servlet() {
super();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String writer = request.getParameter("writer");
//파일 업로드
Part part = request.getPart("partFile1");
String contentDisposition = part.getHeader("content-disposition");
String uploadFileName = getUploadFileName(contentDisposition);
part.write(uploadFileName); //경로에 파일 저장 처리
//출력 확인
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("contentDisposition : "+contentDisposition+"<br>");
out.println("uploadFileName : "+uploadFileName+"<br>");
out.println("작성자 " + writer + "님이 " + uploadFileName + " 파일을 업로드 하였습니다." );
}
//사용 브라우저가 IE인 경우
private String getUploadFileName(String contentDisposition) {
String uploadFileName = null;
String[] contentSplitStr = contentDisposition.split(";");
int lastPathSeparatorIndex = contentSplitStr[2].lastIndexOf("\\");
int lastQutosIndex = contentSplitStr[2].lastIndexOf("\"");
uploadFileName = contentSplitStr[2].substring(lastPathSeparatorIndex + 1, lastQutosIndex);
return uploadFileName;
}
//사용브라우저가 Chrome인 경우
/*private String getUploadFileName(String contentDisposition) {
// TODO Auto-generated method stub
String uploadFileName = null;
String[] contentSplitStr = contentDisposition.split(";");
int firstQutosIndex = contentSplitStr[2].indexOf("\"");
int lastQutosIndex = contentSplitStr[2].lastIndexOf("\"");
uploadFileName = contentSplitStr[2].substring(firstQutosIndex + 1, lastQutosIndex);
return uploadFileName;
}*/
}
- 이전글partUploadForm1.jsp 21.07.15
- 다음글● 파일 업로드 - MultipartRequest 클래스 이용 21.07.15
댓글목록
등록된 댓글이 없습니다.