참고 : 분명히하기 위해 if 문을 수행하는 방법을 알고 있습니다. 실제 콘텐츠 형식을 가져 와서 문자열에 변수에 저장하는 방법이 필요합니다.자바 서버에서 콘텐츠 유형을 반환하십시오.
나는 간단한 자바 서버를 작성했다. 서버가로드하는 파일의 내용 유형을 반환하는 방법을 알지 못했습니다.
contetType과 같은 다른 변수를 기반으로 문자열 변수를 text/html로 설정하는 if 문이 있다는 것을 알고 있습니다. 그래서, 예를 들어 :
String okayreply="HTTP/1.0 200 OK \r\n" +
"Connection: close\r\n" +
"Content-Lenght : "+ file1.length() +
"Content-Type:" +
"\r\n" +
"\r\n" +
"<h2>File Found</h2>\r\n";
System.out.println(okayreply);
그래서 나는 콘텐츠 유형을 찾아서에 저장해야합니다
if (contentType == ".html")
{
String s = "text/html";
}
else if ......
이 서버에서 섹션 (괜찮은 200 응답 코드)입니다 변수. 응답 메시지가 표시 될 때 그 정보를 사용하여 컨텐트 유형을 설정할 수 있습니다.
편집은, 그냥 콘텐츠 유형에 대한 빈 반환 : 또한
File file1 = new File (filename);
String s = "";
int indexOfFinalDot = filename.lastIndexOf(".");
if (indexOfFinalDot > 0) {
String fileExtentsion = filename.substring(indexOfFinalDot).toLowerCase();
if ("html".equals(fileExtentsion)) {
s = "text/html";
}
}
:
String okayreply="HTTP/1.0 200 OK \r\n" +
"Connection: close\r\n" +
"Content-Lenght : "+ file1.length() + "\r\n" +
"Content-Type: " + s +
"\r\n" +
"\r\n" +
"<h2>File Found</h2>\r\n";
System.out.println(okayreply);
난 당신이 여기에 바퀴를 개혁하고 감지 :
당신이 어딘가에 파일 이름이 가정. ServletRequest 클래스 [이미 getContentType 메소드 있음] (http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getContentType()). – gerrytan