package servlet;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.Calendar;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import Ueser.DealString;
import biz.Newbiz;import dto.Dto;
public class Addnews extends HttpServlet { String filename="";
String url="";
String author="";

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String title = request.getParameter("title");
title = new String(title.getBytes("iso-8859-1"),"gb2312");
String content = request.getParameter("content");
content = new String(content.getBytes("iso-8859-1"),"gb2312");
Dto dto = new Dto();
dto = (Dto)request.getSession().getAttribute("user");
author =dto.getUsername();
url = generateHtml(request);
String imageurl = request.getScheme() +"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/upload/"+filename;
Newbiz biz;
try {
biz = new Newbiz();
dto.setTitle(title);
dto.setContent(content);
dto.setAuthor(author);
dto.setUrl(url);
dto.setImageurl(imageurl);
int i = biz.insertnew(dto);
if(i==1)
{
response.sendRedirect("/news/Selectnews");
}else
{
response.sendRedirect("/tongxi/error.html");
}
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
} catch (SQLException e1) {
e1.printStackTrace();
}
}

public String generateHtml(HttpServletRequest request)
{
String createtime = DealString.getDateTime();
String templateFilePath = 
request.getScheme()+ 
"://" 
+ request.getServerName() 
+ ":" 
+ request.getServerPort()
+ request.getContextPath()
+ "/news.html";
//System.out.println(templateFilePath);
String templateContent = "";
FileInputStream fileinputstream;

try {
 fileinputstream = new FileInputStream("F:/work1/news/news.html");
int lenght = fileinputstream.available();
byte bytes[] = new byte[lenght];
fileinputstream.read(bytes);
fileinputstream.close();
templateContent = new String(bytes);
} catch (FileNotFoundException e) {
System.err.println("输入文件找不到");
e.printStackTrace();
} catch (IOException e) {

e.printStackTrace();
}
String title = request.getParameter("title");
String content = request.getParameter("content");
try {
title = new String(title.getBytes("iso-8859-1"),"gb2312");
content = new String(content.getBytes("iso-8859-1"),"gb2312");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String imageurl = request.getScheme() 
+"://"+request.getServerName()
+":"+request.getServerPort()+request.getContextPath()+"/upload/"+filename;

templateContent = templateContent.replaceAll("###title###", title);
templateContent = templateContent.replaceAll("###author###",author);
templateContent = templateContent.replaceAll("###createtime###", createtime);
templateContent = templateContent.replace("###imageurl###", imageurl);
templateContent = templateContent.replaceAll("###content###",content);

Calendar calendar = Calendar.getInstance();
String htmlFileName = String.valueOf(calendar.getTimeInMillis())+ ".html";

String tomcatPath = request.getRealPath("/") + "news\\" + htmlFileName;
String eclipsePath = "F:/work1/news/news/" + htmlFileName;
System.out.println(tomcatPath);
System.out.println(eclipsePath);
 String uurl = request.getScheme()
+ "://" 
+ request.getServerName() 
+ ":" 
+ request.getServerPort()
+ request.getContextPath()
+"/news/"
+ htmlFileName;

try
{

FileOutputStream tomcatOutputStream;
tomcatOutputStream = new FileOutputStream(tomcatPath);
FileOutputStream eclipseOutputStream = new FileOutputStream(eclipsePath);
byte tag_bytes[] = templateContent.getBytes();

tomcatOutputStream.write(tag_bytes);
tomcatOutputStream.close();
eclipseOutputStream.write(tag_bytes);
eclipseOutputStream.close();

catch (FileNotFoundException e) 
{
System.err.println("输出文件找不到");
}
catch (IOException e) 
{
e.printStackTrace();
}
return uurl;
}}