This article describes the JSP implementation of remote file download saved to the server specified directory method. Share to everyone for your reference, specific as follows:
<% @page import= "java.net.*,java.io.*"%> <%!
public boolean Saveurlas (String Photourl, String fileName) {//This method can only user HTTP protocol try {URL url = new URL (photourl);
HttpURLConnection connection = (httpurlconnection) url.openconnection ();
DataInputStream in = new DataInputStream (Connection.getinputstream ());
DataOutputStream out = new DataOutputStream (new FileOutputStream (FileName));
byte[] buffer = new byte[4096];
int count = 0;
while ((count = in.read (buffer)) > 0) {out.write (buffer, 0, count);
} out.close ();
In.close ();
return true;
catch (Exception e) {return false;
} public string Getdocumentat (String urlstring) {//This method is compatible with HTTP and FTP protocol StringBuffer document = new StringBuffer ();
try {URL url = new URL (urlstring);
URLConnection conn = Url.openconnection ();
BufferedReader reader = new BufferedReader (new InputStreamReader (conn).
getInputStream ()));
String line = null; while (line = Reader.readline ())!=NULL) {Document.append (line + "\ n");
} reader.close ();
catch (Malformedurlexception e) {System.out.println ("Unable to connect to URL:" + urlstring);
catch (IOException e) {System.out.println ("IOException when connecting to URL:" + urlstring);
return document.tostring ();
}%> <%//test String Photourl = "Yun_qi_img/43932_750450.jpg";
String fileName = photourl.substring (Photourl.lastindexof ("/"));
String FilePath = "c:/test/";
Boolean flag = Saveurlas (Photourl, FilePath + fileName);
Out.println ("Run ok!\n<br>get URL file" + flag);
%>
I hope this article will help you with JSP programming.