[已解决]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或者别的方式来传递数据

posted @   rldts  阅读(6254)  评论(1编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决
点击右上角即可分享
微信分享提示