smartupload组件上传的中文问题.txt

来自「java在SmartUpload组件上传的中文问题的一个解决方案」· 文本 代码 · 共 24 行

TXT
24 行
字号
很简单的问题,查了半天,不是推荐别的就是什么什么,试验了N个其他组件,还是不行,表单传递的UTF-8汉字有问题,中文文件上传也有问题,郁闷了。
结果不小心就解决了:new String(str.getBytes(),"UTF-8")
不说了,下面做个记录。
 
提交表单
<form action="upload.jsp" method="post" enctype="multipart/form-data">
  <table>
    <tr><td><input type="text" name="newFile"></td></tr>
    <tr><td><input type="file" name="file"><input type="submit" value="upload"></td></tr>
  </table>
</form>
 
处理页面
  com.jspsmart.upload.SmartUpload smartUpload = new com.jspsmart.upload.SmartUpload();
  smartUpload.initialize(pageContext);
  smartUpload.service(request, response);
  smartUpload.upload();
  com.jspsmart.upload.File file = smartUpload.getFiles().getFile(0);
  //取中文文件名
  String fileName = new String(file.getFileName().getBytes(),"UTF-8");
  //取中文表单参数
  String newFileName = new String(smartUpload.getRequest().getParameter("newFile").getBytes,"UTF-8");
  //改名上传
  file.saveAs("upload/" + newFileName,smartUpload.SAVE_VIRTUAL);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?