표준 파일 업로드를 위해 Apache Commons Fileupload 도구를 사용하고 있습니다. 내 문제는 특수 문자 (á, é, ú, 등)가 들어있는 경우 업로드 된 파일의 적절한 파일 이름을 가져올 수 없다는 것입니다. 모두가 변환됩니까? 표지판.서블릿 파일 업로드 파일 이름 인코딩
request.getCharacterEncoding()은 UTF-8을 말하지만 fileItem.getName() 문자열에서 가져온 바이트는 모든 모든 특수 문자에 대해 동일합니다.
무엇이 잘못 되었으면 도와 줄 수 있습니까?
(일부 세부 정보 : Windows에서 파이어 폭스 3.6.12, 웹 로직 10.3을 사용하여)
것은이 내 코드입니다 :
당신은 확인해야합니다public CommandMsg(HttpServletRequest request) {
Enumeration names = null;
if (isMultipart(request)) {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List uploadedItems = upload.parseRequest(request);
Iterator i = uploadedItems.iterator();
FileItem fileItem = null;
while (i.hasNext()) {
fileItem = (FileItem) i.next();
if (fileItem.isFormField()) {
// System.out.println("isFormField");
setAttribute(fileItem.getFieldName(), fileItem.getString());
} else {
String enc = "utf-8";
enc = request.getCharacterEncoding();
String fileName = fileItem.getName();
byte[] fnb = fileItem.getName().getBytes();
byte[] fnb2 = null;
try {
fnb2 = fileItem.getName().getBytes(enc);
String t1 = new String(fnb);
String t2 = new String(fnb2);
String t3 = new String(fnb, enc);
String t4 = new String(fnb2, enc);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
setAttribute(fileItem.getFieldName(), fileItem);
}
}
} catch (FileUploadException ex) {
ex.printStackTrace();
}
// etc..
당신이 봤어 호환 인코딩을 취할 것입니다 기본 인코딩을 취할 것입니다 어떤 인코딩 유형을 설정하지 않는 경우
.. 작동하지 않는 것 같다 파일 이름을 디코딩하거나 디코딩하지 않고 파일 이름을 가져 오려면? 널리 사용되는 라이브러리에는 이러한 문제가 있다는 것이 다소 이상합니다. – gigadot
예, 첫 번째 시도가 다음과 같이 표시됩니다. "String fileName = fileItem.getName();" 그것은 나쁘다. 다른 모든 라인들 (t1..4)은 필사적 인 시도 들일뿐입니다. :-) – jabal