背景:刚开始开发word上传功能时,用的formdata的格式传给后台
问题:后来客户那边说明情况,用的IE8,在用formdata传值时,在后端request不能强转为MultipartHttpServletRequest
调查原因:因为IE8不支持FormData,IE10才支持,因此此方法不兼容IE10以下的浏览器
解决方法:使用jquery.form.js支持的ajaxsubmit进行上传
html代码:
<form id="form1" name="form1" runat="server" method="post" style="position:relative;" enctype="multipart/form-data">
<input type="file" name="fileName" id="fileName" style="cursor:pointer;opacity:0;position: absolute;right: 144px;width: 90px;height: 70px;right:148px\0;filter:Alpha(opacity=0);" />
<button type="button" class="btn btn-default" id="uploadBtn" style="display:none;"><a href="javascript:;"></a>上传</button>
</form>
js代码:必须导入jquery.form.js
$("#uploadDoc").click(function(){
$("#fileName").click();
});
$("#fileName").change(function(){
$('#uploadBtn').click();
});
$('#uploadBtn').click(function(){
if($("#fileName").val()==""){
$.messager.alert("操作提示","请点击浏览按钮 选择文件");
return false;
}
var form = $("form[name=form1]");
var options = {
url:'/flyywsjc/exceljc.do?method=inputExcelContent',
type:'post',
dataType:"json",
success:function(data){
$(".kk_empty_mask .inner").click();
$("#replaceWords").click();
$("#newEditor").val(data.content);
if($("#newEditor").val()!=""){
$("#matchNav").click();
}
}
};
form.ajaxSubmit(options);
});
java代码:
@RequestMapping(params = "method=inputExcelContent")
public void inputExcelContent(@RequestParam("fileName") MultipartFile multipartFile
,HttpServletRequest request, HttpServletResponse response) throws Exception{
// 解决中文乱码问题
request.setCharacterEncoding("utf-8");
//response.setContentType("application/json;charset=utf-8");
response.setContentType("text/plain;charset=utf-8");
PrintWriter out = response.getWriter();
Map<String, String> map = new HashMap<String, String>();
try {
InputStream is = multipartFile.getInputStream();
String filename = multipartFile.getOriginalFilename();
if(filename.indexOf("docx")>-1){
XWPFDocument xdoc = new XWPFDocument(is);
List<XWPFParagraph> paras = xdoc.getParagraphs(); //This list will hold the paragraphs
XWPFWordExtractor ex = new XWPFWordExtractor(xdoc); //To get the words
String content = ""; //This will hold all the text
for(XWPFParagraph p : paras){ //For each paragraph we retrieved from the document
content += p.getText()+" \n"; //Add the text we retrieve to the words string
}
System.out.println(content);
/*XWPFWordExtractor extractor = new XWPFWordExtractor(xdoc);
String content = extractor.getText();*/
this.closeStream(is);
map.put("content", content);
}else if(filename.indexOf("doc")>-1){
WordExtractor extractor = new WordExtractor(is);
String content = extractor.getText();
this.closeStream(is);
map.put("content", content);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (Exception e) {
logger.info("query exceljc error message = :" + e.getMessage());
e.printStackTrace();
}
// 将集合map转换为JSON格式的数据响应到客户端
String cks = JSONObject.fromObject(map).toString();
out.print(cks);
out.close();
}
注意:IE10可以支持application/json格式的Response,也就是说低于IE10版本的IE浏览器都需要使用text/html格式的Response。在Response头中指定Content-type为text/html,是可以解决问题的。这样返回给客户端的是一个JSON字符串(不是JSON对象),无需IE来解析。