Upload file garbled, GBK turn UTF-8

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.