文件上传在Firefox中有效但在IE中使用spring显示异常

时间:2013-02-08 09:01:03

标签: spring-mvc file-upload internet-explorer-9 html-form-post

我在spring mvc上传文件它在firefox中运行正常但在IE9

中引发了以下异常
  com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence.

这就是我的表格:

这就是我的控制器:

@RequestMapping(value = "/CIMtrek_Regional_WhseFormAddSave", method = RequestMethod.POST)
    public ModelAndView CIMtrek_Regional_Whse(
            @RequestParam("CIMtrek_xmlData") String CIMtrek_xmlData,
            @RequestParam("CIMtrek_formName") String CIMtrek_formName,@RequestParam("fileUPload") MultipartFile uploadFile,HttpServletRequest request) {

        UtilService fileUploadService = new UtilService();
        if(!uploadFile.isEmpty()) {
            fileUploadService.saveFile(uploadFile, request.getRealPath(""));
        }
        ViewContent vc = new ViewContent();
        String HTML = vc.getContent(CIMtrek_xmlData, CIMtrek_formName);
        List<String> ls = new ArrayList<String>();
        ls.add(HTML);
        logger.info("Welcome  CIMtrek_Regional_Whse Add!");
        return new ModelAndView("form", "list", ls);
    }

文件保存方法:

public void saveFile(MultipartFile uploadItem, String requestPath) {
        File dir;
        File file;
        try {
            String fileName = null;
            InputStream inputStream = null;
            OutputStream outputStream = null;
            if (uploadItem.getSize() > 0) {
                inputStream = uploadItem.getInputStream();
                fileName = requestPath + "\\resources\\Attachment\\";
                dir = new File(fileName);
                if (!dir.exists()) {
                    dir.mkdirs();
                }
                fileName += uploadItem.getOriginalFilename();
                file = new File(fileName);
                if (!file.exists()) {
                    file.createNewFile();
                }
                outputStream = new FileOutputStream(file);
                int readBytes = 0;
                byte[] buffer = new byte[10000];
                while ((readBytes = inputStream.read(buffer, 0, 10000)) != -1) {
                    outputStream.write(buffer, 0, readBytes);
                }
                outputStream.flush();
                outputStream.close();
                inputStream.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

请帮我解决这个问题。我在提交表单时将文件保存到指定位置。

最好的问候

1 个答案:

答案 0 :(得分:1)

许多Windows程序在保存为UTF-8的任何文档的开头添加字节0xEF,0xBB,0xBF。这是Unicode字节顺序标记(BOM)的UTF-8编码,通常称为UTF-8 BOM,即使它与字节顺序无关。这个漏洞的一个例子是Internet Explorer,它将渲染仅在标准模式下以文档类型声明开头。 所以我想你需要检查并跳过可能的BOM字节

String enc = "ISO-8859-1"; // or NULL to use systemdefault
     FileInputStream fis = new FileInputStream(file);
     UnicodeInputStream uin = new UnicodeInputStream(fis, enc);
     enc = uin.getEncoding(); // check and skip possible BOM bytes
     InputStreamReader in;
     if (enc == null) in = new InputStreamReader(uin);
     else in = new InputStreamReader(uin, enc);

资料来源:UnicodeInputStream