Home > OS >  Under Linux download from the download Windows containing Chinese file failed
Under Linux download from the download Windows containing Chinese file failed

Time:09-17

I need project under Linux, Windows file download the server and parse the file, the result always failed to download,
File for: http://192.178.1.123:80/223/test files. XLSX.
Download for:
Import the Java. IO. BufferedInputStream;
Import the Java. IO. The File;
Import the Java. IO. FileOutputStream;
Import the Java. IO. OutputStream;
The import java.net.HttpURLConnection;
The import java.net.URL;
The import java.net.URLConnection;

Import org, apache log4j. Logger;

The import com. Talkweb. Entity. CustomException;

/* *
* the Java native API can be used to send an HTTP request, namely java.net.URL, java.net.URLConnection, these apis useful, very common,
* but not simple;
*
* 1. Through the uniform resource locator (java.net.URL) for connector (java.net.URLConnection) 2. Set the parameters of the request 3. Send the request
* 4. In the form of the input stream to get return content 5. Close the input stream
*
*
*/
Public class HttpConnectionUtil {

Private static Logger Logger=Logger. GetLogger (HttpConnectionUtil. Class);

/* *
*
* @ param urlPath download path
* @ param fileSavePath download storage directory containing the file name
* @ return return to download file
* @ throws the Exception
*/
Public static File downloadFile (String urlPath, String fileSavePath) throws the Exception {

urlPath=http://192.178.1.123:80/223/test files. XLSX;//Windows server

Logger. The info (" enter the download file tools ");
The File File=null;
BufferedInputStream bin=null;
OutputStream out=null;
Try {
//uniform resource
URL the URL=new URL (urlPath);
//connect the parent class, abstract class
URLConnection URLConnection=url. The openConnection ();
//HTTP connection
HttpURLConnection HttpURLConnection=(HttpURLConnection urlConnection);
//set the request method, the default is GET
HttpURLConnection. SetRequestMethod (" GET ");
//set the character encoding
HttpURLConnection. SetRequestProperty (" Charset ", "utf-8");
//open the resources of communication link to the URL references (if have not set up this connection),
HttpURLConnection. The connect ();

//file size
Int fileLength=httpURLConnection. GetContentLength ();

//file name
String filePathUrl=httpURLConnection. GetURL (). GetFile ();
String fileFullName=filePathUrl. Substring (filePathUrl lastIndexOf (File. SeparatorChar) + 1);
FileFullName=fileFullName. Substring (fileFullName. LastIndexOf ("/") + 1);
Logger. The info (" to start the download file: "+ fileFullName);
Logger. The info (" the file length - & gt;" + fileLength);

Url. The openConnection ();

Bin=new BufferedInputStream (httpURLConnection. GetInputStream ());

String path=fileSavePath;
The file=new file (path);
if (! File. GetParentFile (). The exists ()) {
File. GetParentFile (). The file.mkdirs ();
}
Out=new FileOutputStream (file);
Int size=0;
Int len=0;
Byte [] buf=new byte [1024].
While ((size=bin. Read (buf))!=1) {
Len +=size;
Out. Write (buf, 0, size);
//print the download percentage
If ((len * 100/fileLength) & gt; 50 & amp; & (len * 100/fileLength) & lt; 55) {
Logger. The info (" download the -- -- -- -- -- -- -- & gt; "+ len * 100/fileLength +" % ");
} else if ((len * 100/fileLength) & gt; {
=98)Logger. The info (" download the -- -- -- -- -- -- -- & gt; "+ len * 100/fileLength +" % ");
}
//logger. The info (download the "-- -- -- -- -- -- -- & gt; "+ len * 100/fileLength +" % ");
}
return file;
} the catch (Exception e) {
//TODO Auto - generated the catch block,
Throw new CustomException (e. oString () + "file download abnormal, please check the download link $$" + urlPath);
} the finally {
If (bin!=null) {
Bin. The close ();
}
If (out!=null) {
out.close();
}
}
}

}
  • Related