[已解決]SmartUpload亂碼的問題


1、上傳的文件名如果是中文,那么上傳后就是亂碼

<!-- upload.html -->

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form action=".\jsp\smartupload_demo01.jsp" method="post" enctype="multipart/form-data">
        請選擇要上傳的文件:<input type="file" name="pic">
        <input type="submit" value="上傳">
    </form>
</body>
</html>

<!-- upload.jsp -->

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.jspsmart.upload.SmartUpload" %>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
    String rootPath = application.getRealPath("/");
    // System.out.println(rootPath); // @Debug
    SmartUpload smart = new SmartUpload() ;
    smart.initialize(pageContext) ;    // 初始化上傳操作
    smart.upload() ;            // 上傳准備
    smart.save(rootPath + "\\upload") ;    // 文件保存
%>
</body>
<h2>上傳成功,<a href="..\smartupload_demo01.html">返回</a></h2>
</html>

2、如果是如下這種情況,獲取的String parameter "name"也是亂碼(輸入中文的話)

upload.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ page import="com.jspsmart.upload.SmartUpload" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<%
    request.setCharacterEncoding("UTF-8") ;
%>
<%
    String rootPath = application.getRealPath("/");
    // System.out.println(rootPath); // @Debug
    SmartUpload smart = new SmartUpload() ;
    smart.initialize(pageContext) ;    // 初始化上傳操作
    smart.upload() ;            // 上傳准備
    String name = smart.getRequest().getParameter("uname");
    // System.out.println(name); // @Debug SmartUpload
    smart.save(rootPath + "\\upload") ;    // 文件保存
%>
<h2>上傳成功,<a href="..\smartupload_demo02.html">返回</a></h2>
<h2>姓名:<%=name %></h2><br>
<h2>request.getParameter("uname"): <%=request.getParameter("uname") %></h2>
</body>
</html>

upload.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form action=".\jsp\smartupload_demo02.jsp" method="post" enctype="multipart/form-data">
        姓名:<input type="text" name="uname"><br>
        照片:<input type="file" name="pic"><br>
        <input type="submit" value="上傳">
        <input type="reset" value="重置">
    </form>
</body>
</html>

因為使用SmartUpload需要對form使用enctype,導致數據按照純二進制的方式提交,因此會出現編碼問題,而SmartUpload本身的API又沒有提供對編碼的解決方案。

3、解決方案

使用如下代碼,完美解決:

SmartUpload smart = new SmartUpload() ;
smart.setCharset("UTF-8"); // 或者你需要的編碼

搜了一下

一種是修改SmartUpload的源碼,讓SmartUpload內部解決編碼的轉換問題,自己編譯了再用

第二種就是選擇另一款上傳工具,比如fileupload

另外,采用JS或者別的方式來傳遞數據


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM