ExtJs上传中文文件名乱码,观察请求。

ExtJs6上传乱码从后台无法解决,因为文件名请求里面就已经乱码了,后台无法解码。

除非请求参数正确没有乱码,后台因为编码设置不一样,可以通过后台处理乱码

这里的思路使用ExtJs的Form隐藏域。

前端使用Base64加密,后台用Base64解密。

这里加密的时间很巧妙,发送请求前文件名并未乱码,把这个取出来用Base64加密(乱码的加密成Base64是徒劳的)

放入空的form里面,带给后台

/**
* @author cjy
* @Date 2018/6/5 9:47.
*/
Ext.define('Admin.view.eduQuestion.ExcelImportForm', {
extend: 'Ext.window.Window',
xtype: 'excelImportForm', title: '导入表格', requires: [
'Admin.view.eduQuestion.ExcelImportFormController',
'Ext.form.Panel',
'Ext.form.field.ComboBox',
'Ext.form.field.Text',
'Ext.form.field.TextArea',
'Ext.layout.container.Fit'
], layout: 'fit', modal: true,
height: 200,
width: 370, controller: 'excelImportForm', items: [{
flex: 1,
xtype: 'form',
reference: 'form',
modelValidation: true,
defaults: {
labelAlign: 'left',
margin: 10,
msgTarget: 'side'
},
items: [{
xtype: 'textfield',
hidden:true,
name:'fileName',
id:'fileName'
},{
xtype: 'filefield',
name: 'excelPath',
id:'excelPath',
fieldLabel: '表格',
labelWidth: 50,
msgTarget: 'side',
anchor: '100%',
accept: 'xls/xlsx',
buttonText: '选择Excel表格...',
validator: function (value) {
if(value==''){
return true;
}
var arr = value.split('.');
if (arr[arr.length - 1] == 'xls' || arr[arr.length - 1] == 'xlsx') {
return true;
} else {
return '必须选择xls或者xlsc格式的Excel!';
}
}
}]
}],
buttons: [{
text: '确定',
handler: 'editExcel'
}, {
text: '取消',
handler: 'closeWindow'
}]
});

在Controller.js里面对form进行操作,关键代码已标红

/**
* @author cjy
* @Date 2018/6/5 9:47.
*/
Ext.define('Admin.view.eduQuestion.ExcelImportFormController', {
extend: 'Admin.view.BaseViewController', alias: 'controller.excelImportForm', /**Excel上传
* @param {Ext.button.Button} component
* @param {Event} e
*/
editExcel: function () {
var me = this,
window = me.getView(),
form = window.down('form');
if (!form.isValid()) {
return false;
} var ff = Ext.getCmp('excelPath')
var ffv = ff.getValue('filename')
var index = ffv.lastIndexOf('\\')
var ffv = Ext.util.Base64.encode(ffv.substring(index+1,ffv.length))
Ext.getCmp('fileName').setValue(ffv);
if (window.action === 'uploadExcel') {
successMsg = '上传成功';
submitUrl=Common.Config.requestPath('EduQuestion', 'importExcelForEduQuestion');
} else {
Ext.Msg.alert('温馨提示', '表单操作错误,请联系管理员');
return;
}
form.submit({
//method : 'get',默认是post,指定没用
url: submitUrl,
waitMsg: '正在上传Excel并导入,请稍等...',
success: function(fp, o) {
Common.util.Util.toast(successMsg);
me.getView().close();
},
failure: function(form, action) {
Common.util.Util.toast(successMsg);
me.getView().close();
}
});
}, /**关闭 userWindow
* @param {Ext.button.Button} component
* @param {Event} e
*/
closeWindow: function () {
this.getView().close();
} });

后台接收,本来只用MultipartFile就可以接收到文件名和文件内容,现在需要两个参数,一个接收文件名,另一个接收文件内容

@RequestMapping(value = "/importExcelForEduQuestion")
@ResponseBody
//@RequiresPermissions("eduQuestionBank:importExcelForEduQuestion")
public Map<String,Object> importExcelForEduQuestion(@RequestParam("excelPath") MultipartFile file,@RequestParam("fileName") String fileName) throws IOException {
//如果文件不为空,写入上传路径
if(!file.isEmpty()) {
//上传文件路径
//上传文件名
BASE64Decoder decoder = new BASE64Decoder();
String filename = new String(decoder.decodeBuffer(fileName),"UTF-8");
FileToolUtil.ifCreateNewFile(UPLOADED_FILE_PATH,filename);
//将上传文件保存到一个目标文件当中
try {
file.transferTo(new File(UPLOADED_FILE_PATH + File.separator + filename));
} catch (IOException e) {
e.printStackTrace();
return ResultUtil.createFailResult("上传失败");
}
return ResultUtil.createSuccessResult();
} else {
return ResultUtil.createFailResult("上传文件为空");
}
}

补充文件FoolUtil里的方法

public static File createNewFile(String pathFileName) throws IOException {
File outFile = new File(pathFileName);
File parentFile = outFile.getParentFile();
if (!parentFile.exists()){
parentFile.mkdirs();//不存在则创建父目录
}
if(!outFile.exists()) {
outFile.createNewFile();
}
return outFile;
}

完美解决ExtJs6上传中文文件名乱码,后端SpringMVC的更多相关文章

  1. PHP 中move_uploaded_file 上传中文文件名失败

    项目需要上传文件名保持不变,发现上传中文失败:错误如下: move_uploaded_file(public/upload/files//-/\开密二次开发.rar): failed to open ...

  2. 通过freemarker生成一个word,解决生成的word用wps打开有问题的问题,解决出word时中文文件名乱码问题,解决打开出word时打开的word出现问题的问题,出图片,解决动态列表

     通过freemarker制作word比较简单 步骤:制作word模板.制作方式是:将模板word保存成为xml----在xml的word模板中添加相应的标记----将xml的word文件的后缀名 ...

  3. Flask下如何处理Requests 上传中文文件名的问题

    一.问题的由来     最近有个项目,叫做文档服务资源中心,类似于七牛,为各个业务系统提供统一的文件资源服务,包括文件的存储.操作管理.下载.预览等.在做文件存储的时候,遇到了这个当指定上传的文件名为 ...

  4. php 解决上传中文文件名时出现乱码的问题

    有时候上传文件是中文的文件名会出现乱码, 可以在移动文件时使用icov('utf-8','gb2312',filename)转换 代码: <?php //header('Content-type ...

  5. 解决windows下nginx中文文件名乱码

    我的根目录文件夹放在d盘work文件夹下,一般这样配置 nginx\conf\nginx.conf location / { root D:/work; index index_bak.html; a ...

  6. 解决 linux 下面解压缩 中文文件名乱码问题的方法 unzip -O CP936

    Linux 解压缩 zip包中文目录出现乱码的问题. 出现问题如图示: unzip -O CP936 xxx.zip 用这种方式处理一下就好了.

  7. ASP.Net上传中文文件乱码

    只要在Head中添加即可解决:<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />

  8. Servlet 起航 文件上传 中文文件名下载

    @WebServlet(name = "ticketServlet",urlPatterns = {"/tickets"},loadOnStartup = 1) ...

  9. php上传中文文件文件名乱码问题

    php上传文件是最最基础的一个技术点,但是深入进去也有不少问题需要解决,这不,上传中文文件后,文件名变成了乱码. 下面是问题代码,很简单: 1.问题代码 html部分: <html> &l ...

随机推荐

  1. Centos 7 修改默认的运行级别

    Runlevel System State 0 Halt the system 1 Single user mode 2 Basic multi user mode 3 Multi user mode ...

  2. Transaction And Lock--两种方式实现可重复读

    一些需求要求两次查询数据之间不允许数据被修改,即可重复读取 可重复读REPEATABLE READ与串行化SERIALIZABLE的区别在于串行化要求满足该查询的数据不被修改且无新满足该查询条件的数据 ...

  3. 实战iOS7之NSURLSession

    NSURLSession VS NSURLConnection NSURLSession可以看做是NSURLConnection的进化版,其对NSURLConnection的改进点有: * 根据每个S ...

  4. consul ACL2

    简介 Consul有多个组件,但是整体上,consul通常作为服务发现工具来使用. Consul主要由以下特点: 服务发现 健康检查 KV存储 多数据中心 Consul一般与zookeeper,ser ...

  5. Visual Studio 2015 Update 2 发布

    2016年3月30日,微软发布了Visual Studio 2015 Update 2 . 更新内容: Visual Studio  Visual Studio Tools for Apache Co ...

  6. Tips on rendering interiors

    http://www.evermotion.org/tutorials/show/9824/making-of-morning-breakfast-tip-of-the-week http://www ...

  7. “全栈2019”Java第十八章:一元运算符

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  8. leetcode-137-Single Number II-第二种解法

    题目描述: 详细的题目描述见上一篇博客<leetcode-137-Single Number II-第一种解法>,这里简单说一下. 有一个数组,所有元素都出现了三次,除了一个元素只出现了一 ...

  9. Ubuntu如何配置网桥 Ubuntu系统配置网桥详细教程

    注意:如果是在ubuntu桌面版本上使用,图形化控制与ifupdown配置不兼容.如果使用ifupdown来配置,需要禁止使用图形化控制. 本文经过本人结合网络内容亲身实践,配置通了ifupdown ...

  10. 干掉Vivado幺蛾子(1)-- Xilinx Tcl Store

    目录 1. 安装Xilinx Tcl Store 2. 手动更新 2.1 下载库 2.2 修改环境变量 参考文献: 最近在跟着高亚军老师的分析文章来学习Xilinx最近发布的<UltraFast ...