private static final String FILEPATH = "/opt/nginx/html/banner/";
private static final String RETURNPATH = "/banner/"; private static final String IMAGEPATH="/image_app/scp_image/bannerimg/"; private static final String TEMPLATE_PATH = "/opt/nginx/html/banner/modelHtml.html"; @RequestMapping("/submit")
@ResponseBody
public Map<String, Object> submit(String imageUrl) { String str = "";
StringBuffer buffer = new StringBuffer();
String uuidFilePath = UUID.randomUUID().toString() +".html";
try { /*URL url = new URL(SERVICE_PATH+SAVEHTMLPATH);
HttpURLConnection httpUrl = (HttpURLConnection)url.openConnection();
InputStream is = httpUrl.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is,"utf-8"));
String line;
while ((line = br.readLine()) != null) {
buffer.append(line+"\n");
}
is.close();
br.close();*/ File file = new File(TEMPLATE_PATH);
if (file.isFile() && file.exists()) {
InputStreamReader read = new InputStreamReader(new FileInputStream(file), "utf-8");
BufferedReader bufferedReader = new BufferedReader(read);
String line = null;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
buffer.append(line+"\n");
}
bufferedReader.close();
}else{
System.out.println("system erro");
} str = buffer.toString().replaceAll("###imageUrl###", IMAGEPATH+imageUrl); String saveHtmlFile = FILEPATH + uuidFilePath;
BufferedWriter bufferedWriter = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(saveHtmlFile), "UTF-8"));
bufferedWriter.write(str);
bufferedWriter.newLine();// 换行
/*
* * 刷新该流的缓冲。 关键的一行代码。如果没有加这行代码。数据只是保存在缓冲区中。没有写进文件。 加了这行才能将数据写入目的地。
* *
*/
bufferedWriter.flush();
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
} return APIUtil.toMap(HttpStatus.SUCCESS.getValue(), true, RETURNPATH+uuidFilePath);
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL; public class MakeHtml { public static boolean JspToHtmlFile(String filePath, String saveHtmlFile) {
String str = ""; StringBuffer buffer = new StringBuffer(); /*try {
String tempStr = "";
InputStreamReader isr = new InputStreamReader(new FileInputStream(filePath), "UTF-8");
BufferedReader br = new BufferedReader(isr);
while ((tempStr = br.readLine()) != null){
str = str + tempStr +"\n";
}
} catch (IOException e) {
e.printStackTrace();
return false;
}*/ try {
URL url = new URL(filePath);
HttpURLConnection httpUrl = (HttpURLConnection)url.openConnection();
InputStream is = httpUrl.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is,"utf-8"));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
buffer.append(line+"\n");
}
is.close();
br.close();
} catch (Exception e) { } try {
str = buffer.toString().replaceAll("###imageUrl###","http://img.zcool.cn/community/0142135541fe180000019ae9b8cf86.jpg@1280w_1l_2o_100sh.png");
//必须设置编码格式不然会出现乱码
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(saveHtmlFile),"UTF-8"));
bufferedWriter.write(str);
bufferedWriter.newLine();//换行
bufferedWriter.flush();
bufferedWriter.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
} public static void main(String[] args) {
long begin = System.currentTimeMillis();
String url = "http://xxx/banner/modelHtml.html";// 模板文件地址
String savepath = "d:/" + begin + ".html";// 生成文件地址
JspToHtmlFile(url, savepath);
System.out.println("用时:" + (System.currentTimeMillis() - begin) + "ms");
}
}