如何获取非英文文件名的文件路径?

发布于 2025-01-08 23:41:27 字数 4469 浏览 0 评论 0原文

我制作了一个使用 MultipartRequest 上传文件的页面。 我检查上传的文件没有问题。 然后我在下载时遇到问题。

如果文件名是韩语,则程序无法读取文件的路径。我尝试在 server.xml 的 Connector 标记处设置“URIEncoding =“UTF-8”,但它不起作用。

如何获取名称为韩语的文件的路径?

[第一个 Jsp 文件]

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form action="02_fileUpload.jsp" method="post" enctype="multipart/form-data">
        <table border=1>
            <tr>
                <td colspan=2 align="center"><h3>file upload form</h3></td>
            </tr>
            <tr>
                <td>uploader</td>
                <td><input type="text" id="uploder" name="uploader"></td>
            </tr>
            <tr>
                <td>title</td>
                <td><input type="text" id="title" name="title"></td>
            </tr>
            <tr>
                <td>file name1</td>
                <td><input type="file" name="file1"></td>
            </tr>
            <tr>
                <td>file name2</td>
                <td><input type="file" name="file2"></td>
            </tr>
            <tr>
                <td colspan="2" align="center"><input type="submit" value="send"></td>
            </tr>
        </table>
    </form>
</body>
</html>

[第二个 Jsp 文件]

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.oreilly.servlet.MultipartRequest" %>
<%@ page import="com.oreilly.servlet.multipart.DefaultFileRenamePolicy" %>
<%@ page import="java.util." %>
<%
    String uploadPath = request.getRealPath("upload");

    int size = 101024*1024;
    String uploader = "";
    String title = "";
    String filename1= "";
    String filename2= "";

    try{
        MultipartRequest multi = 
                new MultipartRequest(request, uploadPath, size, "utf-8", new DefaultFileRenamePolicy());
        uploader = multi.getParameter("uploader");
        title = multi.getParameter("title");

        Enumeration files = multi.getFileNames();
        String file1 = (String)files.nextElement();
        filename1 = multi.getFilesystemName(file1);


        String file2 = (String)files.nextElement();
        filename2 = multi.getFilesystemName(file2);


    }catch(Exception e){
        e.printStackTrace();
    }
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
    <form name="filecheck" action="03_fileCheck.jsp" method="post">
        <input type="hidden" name ="uploader" value="<%=uploader%>">
        <input type="hidden" name ="title" value="<%=title%>">
        <input type="hidden" name ="filename1" value="<%=filename1%>">
        <input type="hidden" name ="filename2" value="<%=filename2%>">
    </form>
    <a href = "#" onclick="javascript:filecheck.submit()">go to check for upload files</a>
</body>
</html>

[第三个 Jsp 文件]

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
    request.setCharacterEncoding("utf-8");
    String uploader = request.getParameter("uploader");
    String title = request.getParameter("title");
    String filename1 = request.getParameter("filename1");
    String filename2 = request.getParameter("filename2");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>check for upload files and download them</title>
</head>
<body>
    uploader: <%=uploader%><br>
    title:<%=title%><br>
    file name1: <a href ="upload/<%=filename1%>"><%=filename1%></a><br>
    file name2: <a href ="upload/<%=filename2%>"><%=filename2%></a><p>
</body>
</html>

当我下载名称为 KR 的文件时出现此问题: 404 not找到了

I made a page for uploading files with MultipartRequest.
There is no problem when i check the uploaded files.
and then I have a problem when I'm downloading it.

If the file's name is in Korean, the program cannot read the path of the file. I tried to set "URIEncoding="UTF-8" at Connector tag at server.xml but it didn't work.

How can I get the path of the file which name is in Korean?

[first Jsp file]

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form action="02_fileUpload.jsp" method="post" enctype="multipart/form-data">
        <table border=1>
            <tr>
                <td colspan=2 align="center"><h3>file upload form</h3></td>
            </tr>
            <tr>
                <td>uploader</td>
                <td><input type="text" id="uploder" name="uploader"></td>
            </tr>
            <tr>
                <td>title</td>
                <td><input type="text" id="title" name="title"></td>
            </tr>
            <tr>
                <td>file name1</td>
                <td><input type="file" name="file1"></td>
            </tr>
            <tr>
                <td>file name2</td>
                <td><input type="file" name="file2"></td>
            </tr>
            <tr>
                <td colspan="2" align="center"><input type="submit" value="send"></td>
            </tr>
        </table>
    </form>
</body>
</html>

[second Jsp file]

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.oreilly.servlet.MultipartRequest" %>
<%@ page import="com.oreilly.servlet.multipart.DefaultFileRenamePolicy" %>
<%@ page import="java.util." %>
<%
    String uploadPath = request.getRealPath("upload");

    int size = 101024*1024;
    String uploader = "";
    String title = "";
    String filename1= "";
    String filename2= "";

    try{
        MultipartRequest multi = 
                new MultipartRequest(request, uploadPath, size, "utf-8", new DefaultFileRenamePolicy());
        uploader = multi.getParameter("uploader");
        title = multi.getParameter("title");

        Enumeration files = multi.getFileNames();
        String file1 = (String)files.nextElement();
        filename1 = multi.getFilesystemName(file1);


        String file2 = (String)files.nextElement();
        filename2 = multi.getFilesystemName(file2);


    }catch(Exception e){
        e.printStackTrace();
    }
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
    <form name="filecheck" action="03_fileCheck.jsp" method="post">
        <input type="hidden" name ="uploader" value="<%=uploader%>">
        <input type="hidden" name ="title" value="<%=title%>">
        <input type="hidden" name ="filename1" value="<%=filename1%>">
        <input type="hidden" name ="filename2" value="<%=filename2%>">
    </form>
    <a href = "#" onclick="javascript:filecheck.submit()">go to check for upload files</a>
</body>
</html>

[Third Jsp file]

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
    request.setCharacterEncoding("utf-8");
    String uploader = request.getParameter("uploader");
    String title = request.getParameter("title");
    String filename1 = request.getParameter("filename1");
    String filename2 = request.getParameter("filename2");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>check for upload files and download them</title>
</head>
<body>
    uploader: <%=uploader%><br>
    title:<%=title%><br>
    file name1: <a href ="upload/<%=filename1%>"><%=filename1%></a><br>
    file name2: <a href ="upload/<%=filename2%>"><%=filename2%></a><p>
</body>
</html>

This comes out when I download files which name is in KR : 404 not Found

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文