2014-12-04 135 views
-3

我希望通過將文件的URL(該文件可以是圖像,Xhtml或Css)粘貼到JSP的表單中,可以從Internet上下載並保存本地。請你能幫助我嗎?從Spring中的URL下載

回答

4
you can use this to open URL in the browser and save into the file location. 
<% 
String site= contain the string(URL); 
response.setStatus(response.SC_MOVED_TEMPORARILY); 
response.setHeader("Location", site); 
File file = new File("/Users/asdf.xml"); 
FileWriter fr = null; 
BufferedWriter br = null; 
URL url = new URL(site); 
BufferedReader reader = new BufferedReader 
(new InputStreamReader(url.openStream())); 

fr = new FileWriter(file); 
br = new BufferedWriter(fr); 
String line; 
while ((line = reader.readLine()) != null) { 
System.out.println(line); 
br.write(line); 
br.newLine(); 
} 
reader.close(); 
br.close(); 
%>