Recently encountered such a problem, I upload files do not convert the character set Windows default GBK, the results when I need to read the file display with UTF-8 turn of course will appear garbled, in order to solve this problem, I will upload files together also turned into UTF-8 so that will not garbled, The upload-time conversion character Set method is as follows:
String root = FilePath;//Upload PathFile RootFile =NewFile (root);//path does not exist first create pathif(!rootfile.exists ()) {Rootfile.mkdirs ();}//get suffix typeString suffix =domain.getsuffixname ();//New file nameString strnewname = Domain.getscriptname (). Trim () + domain.getversionname (). Trim () + "." +Suffix;domain.setscripturl ("/" +strnewname); File DestFile; String Destfilepath=root+ "/" +Strnewname;destfile=NewFile (root, strNewName); OutputStream Outputstream;outputstream=NewFileOutputStream (destfile);//file is uploaded files, stored as temporary TMPFile File =Domain.getfile (); String Context= ""; InputStreamReader Isr;isr=NewInputStreamReader (NewFileInputStream (file), "GBK"); BufferedReader BR=NewBufferedReader (ISR); String Line; while(line = Br.readline ())! =NULL) {Context+ = line + "\ r \ n"; System.out.println (line);} Br.close ();byte[] content =context.getbytes (); context=NewString (Content, "UTF-8"); BufferedWriter writer; FileOutputStream Fos=NewFileOutputStream (Destfilepath,true); System.out.println (Root+"/"+strNewName); writer=NewBufferedWriter (NewOutputStreamWriter (FOS, "UTF-8") ; writer.append (context); Writer.close ();
Upload file garbled, GBK turn UTF-8