把一个字符串处理成一个合法的文件名. c#, java都可用

 

上传文件的时候,我们很头疼的对文件的中文命名,一个字符串经常不合法导致文件存储不上,该方法经过多次改造,分享给各位。该文件位于: 开源驰骋工作流bp框架里面。

 

public static void WriteFile(string file, string Doc)

        {

            System.IO.StreamWriter sr;

           

            try

            {

                if (System.IO.File.Exists(file))

                    System.IO.File.Delete(file);

                //sr = new System.IO.StreamWriter(file, false, System.Text.Encoding.GetEncoding("GB2312"));

                sr = new System.IO.StreamWriter(file, false, System.Text.Encoding.UTF8);

            }

            catch (Exception ex)

            {

                throw new Exception("@文件:" + file + ",错误:" + ex.Message);

            }

 

            sr.Write(Doc);

            sr.Close();

        }

 

 

 

 

欢迎使用 驰骋bpm工作流引擎 http://doc.ccbpm.cn, 官方网站: http://ccflow.org

 

 

 

posted @ 2021-05-06 09:06  开源驰骋工作流-表单  阅读(99)  评论(0编辑  收藏  举报