Non english file names saved in server shows as?

╄→尐↘猪︶ㄣ 提交于 2019-12-11 17:05:23

问题


When i upload non english files from an application deployed in portal server .After the file is saved .The file name changes to ??????. i have added the savefile function code for reference which gets called on saving file

The Non english filename gets printed correctly in the logger wherever the logger is given to print the filename . But the filename gets saved in the server as ????.

public void saveFile(java.io.InputStream is, String fileName, String fpath, String remoteUser, String uiniqueId, String fileSize)
{
    logger.info("fileName: "+fileName);
    logger.info("fpath: "+fpath);
    logger.info("remoteUser: "+remoteUser);
    logger.info("uiniqueId: "+uiniqueId);

    //Define upload directory, notice that permissions to write are needed for upload directory
    String uploadDir = getBaseDir() + fpath;
    String fullFileName = uploadDir+"/"+fileName;

    String filePath = uploadDir + "/"+uiniqueId;
    logger.info("filePath: " + filePath);
    //write to disk
    //Process a file upload
    File uploadedFile = new File(fullFileName);
    File bFile = new File(uploadDir+"/"+ ".mpbac." + fileName);

    if(fileName.lastIndexOf(".war")==-1) {
        if(uploadedFile.exists()) {
            if(bFile.exists()) {
                //if uploaded file is different from backup file
                if(uploadedFile.lastModified()!=bFile.lastModified()) {
                    File orig_uploadedFile = new File(fullFileName);
                    File thisbackupFile = new File(uploadedFile.getParent() + "/.mpbac."+fileName);
                    thisbackupFile.delete();
                    orig_uploadedFile.renameTo(thisbackupFile);
                    logger.info("orig_uploadedFile:" + orig_uploadedFile.getAbsolutePath());
                    logger.info("thisbackupFile:" + thisbackupFile.getAbsolutePath());
                }
            }
        }
    }

    OutputStream os = null;
    try
    {
        File dst = new File(filePath);

        if (dst.exists()) {
            os = new BufferedOutputStream(new FileOutputStream(dst, true),
                    40960);
        }else {
            os = new BufferedOutputStream(new FileOutputStream(dst),
                    40960);
        }
        byte[] buffer = new byte[40960];
        int len = 0;
        while ((len = is.read(buffer)) > 0) {
            os.write(buffer, 0, len);
        }
        logger.info("File touch starting...: " +filePath);
        FileUtils.touch(new File(filePath));

        logger.info("File write done successfully...: " +filePath);

    }
    catch (IOException ex) {
        ex.printStackTrace();
        logger.log(Level.SEVERE,"saveFile>> IOException  "+getStackTrace(ex));
    }
    catch(Exception exp)
    {
        logger.log(Level.SEVERE,"saveFile>> Exception "+getStackTrace(exp));
    }
    finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException ex) {
            }
        }
        if (os != null) {
            try {
                os.close();
            } catch (IOException ex) {
            }
        }
    }

    //Renaming and writing backup files
    try
    {
        //Renaming files
        File file = new File(filePath);
        logger.info("Testing file length::" + file.length());
        if (file.length() >= Integer.parseInt(fileSize)) {
            File renameFile = new File(fullFileName);
            boolean isDeleted = false;
            //if same file already existing, delete it first.
            if(renameFile.exists())
            {
                isDeleted = renameFile.delete();
                logger.info("Existing file- "+fullFileName+" has been deleted");
            }
            else
            {
                isDeleted = true;
                logger.info(fullFileName+" NOT exist to delete.");
            }
            //Renaming started
            if(isDeleted)
            {
                boolean isSuccess = file.renameTo(new File(fullFileName));
                if(isSuccess)
                {
                    logger.info("File renamed to "+fullFileName);
                }
                else
                {
                    logger.info("NOT able to rename- "+fullFileName);
                }
            }
        }

        Action action = new Action();
        action.action = Action.IMPORT;
        action.user = remoteUser;
        action.path = fpath;
        action.description = action.user + " importing " + fileName + " to " + action.path + " in Import Operation";
        AuditManager.getInstance().logAction(action);

        //create new backup file
        if(fileName.lastIndexOf(".war")==-1) {
            if(!bFile.exists()) {
                logger.info("Attempting to new backup file for " + fullFileName);
                File uploadedFiletemp = new File(fullFileName);
                //rename current for backup
                String path = uploadedFiletemp.getParent();

                File backupFile = new File(path + "/.mpbac."+fileName);
                //FileUtils.copyFile(uploadedFile,backupFile);
                logger.info("New Backup file write done successfully : " + path + "/.mpbac_"+fileName);
            }
        }
    }
    catch(Exception exp)
    {
        logger.log(Level.SEVERE,"saveFile>> Exception while renaming or writing backup files- "+getStackTrace(exp));
    }


}

来源:https://stackoverflow.com/questions/57349661/non-english-file-names-saved-in-server-shows-as

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!