PartUploadPro1Servlet.java > MODEL

본문 바로가기

사이트 내 전체검색

뒤로가기 MODEL

PartUploadPro1Servlet.java

페이지 정보

작성자 관리자 작성일 21-07-15 19:31 조회 487 댓글 0

본문

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;


}*/

}

추천0

댓글목록 0

등록된 댓글이 없습니다.


Copyright © dancePKT. All rights reserved.
PC 버전으로 보기