Direct io read/write
[Java]
Package net. spring. service;
Import java. io. BufferedReader;
Import java. io. File;
Import java. io. FileOutputStream;
Import java. io. InputStream;
Import java. io. InputStreamReader;
Import java. io. PrintWriter;
Import java.net. HttpURLConnection;
Import java.net. URL;
Import java. util. Date;
Import org. aspectj. weaver. patterns. ThisOrTargetAnnotationPointcut;
Public class toHtml {
Private static long star = 0;
Private static long end = 0;
Private static long ttime = 0;
// Return html code
Public static String getHtmlCode (String httpUrl ){
Date before = new Date ();
Star = before. getTime ();
String htmlCode = "";
Try {
InputStream in;
URL url = new java.net. URL (httpUrl );
HttpURLConnection connection = (HttpURLConnection) url
. OpenConnection ();
Connection = (HttpURLConnection) url. openConnection ();
Connection. setRequestProperty ("User-Agent", "Mozilla/4.0 ");
Connection. connect ();
In = connection. getInputStream ();
Java. io. BufferedReader breader = new BufferedReader (
New InputStreamReader (in, "UTF-8 "));
String currentLine;
While (currentLine = breader. readLine ())! = Null ){
HtmlCode + = currentLine;
}
} Catch (Exception e ){
E. printStackTrace ();
} Finally {
Date after = new Date ();
End = after. getTime ();
Ttime = end-star;
System. out. println ("execution time:" + ttime + "second ");
}
Return htmlCode;
}
// Store files
Public static synchronized void writeHtml (String filePath, String from
){
String info = getHtmlCode (from );
PrintWriter pw = null;
Try {
File writeFile = new File (filePath );
Boolean isExit = writeFile. exists ();
If (isExit! = True ){
File file = new File (writeFile. getParent ());
If (! File. exists ())
File. mkdirs ();
WriteFile. createNewFile ();
} Else {
WriteFile. delete ();
WriteFile. createNewFile ();
}
Pw = new PrintWriter (new OutputStreamWriter (new FileOutputStream (filePath, true), "UTF-8 "));
<Span style = "white-space: pre"> </span>
Pw. println (info );
Pw. close ();
} Catch (Exception ex ){
System. out. println (ex. getMessage ());
} Finally {
Pw. close ();
}
}
Public static void main (String [] args ){
String url = "http: // localhost: 8080/blog ";
WriteHtml ("c:/demo.html", url );
}
}
[Java]
// Obtain static articles
@ RequestMapping ("/html/{id }")
Public String getHtmlAticle (@ PathVariable int id, HttpServletRequest request, HttpServletResponse res ){
String path = request. getSession (). getServletContext (). getRealPath ("/html") + "\" + id + ". htm ";
String from = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + request. getContextPath () + "/";
System. out. println ("path" + path );
System. out. println ("s:" + from );
File file = new File (path );
If (! File. exists ())
{
ToHtml. writeHtml (path, from + "article.html? Article_id = "+ id );
}
Return "redirect:/html/" + id + ". htm ";
}
Note that the path of the css image should be changed to the path relative to the root directory.
Author: cq361106306