0

我在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();
        }
    }

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

此致

4

1 回答 1

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

于 2013-02-08T13:31:00.280 回答