10. articleDAO持久化的文章物件
/**
* 插入一篇文章
* @param article 需要做持久化的文章物件
*/
public void insert(Article article){
String sql="insert into blog_article values(?,?,?,?,?,?,?,?)";
PreparedStatement psmt = null;
try {
psmt = conn.prepareStatement(sql);
psmt.setString(1, article.getArticleId());
psmt.setString(2, article.getUserId());
psmt.setString(3, article.getArticleName());
psmt.setString(4, article.getArticleContentEx());
psmt.setTimestamp(5,new Timestamp(article.getLastModify().getTime()));
psmt.setTimestamp(6,new Timestamp(article.getCreateDate().getTime()));
psmt.setInt(7, article.getClickNumber());
psmt.setString(8, article.getAuthorName());
psmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
psmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
11.查詢所有的使用者
public void list(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Connection conn = DataSourceFactory.getDataSource().getConnection();
UserDAO userDAO = new UserDAO(conn);
users = userDAO.findAll();
request.setAttribute("users", users);
conn.close();
request.getRequestDispatcher("user_list.jsp").forward(request,
response);
} catch (SQLException e) {
e.printStackTrace();
}
}
12. //Enumeration列舉型別
String uri = request.getRequestURI().trim();
int start = uri.lastIndexOf("/");
int end = uri.lastIndexOf(".");
String cmd = uri.substring(start + 1, end);
String values = props.getProperty(cmd);
String[] sc = values.split(",");
String clzName = sc[0];
String methodName = "process";
if (sc.length > 1 && sc[1] != null && sc[1] != "") {
methodName = sc[1];
try {
Action action = (Action)Class.forName(clzName).newInstance();
Enumeration names=request.getParameterNames();
while(names.hasMoreElements()){
String name=(String) names.nextElement();
String value=request.getParameter(name);
Field f=action.getClass().getDeclaredField(name);
String setMethodName="set"+name.substring(0,1)+name.substring(1);
Method method=action.getClass().getMethod(setMethodName, f.getType());
if(f.getType()==int.class||f.getType()==Integer.class){
method.invoke(setMethodName, Integer.parseInt(value));
}else if(f.getType()==float.class||f.getType()==Float.class){
method.invoke(setMethodName, Float.parseFloat(value));
}else if(f.getType()==Date.class){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
method.invoke(setMethodName, sdf.parse(value));
}else{
method.invoke(setMethodName, value);
}
13.xml的驗證檔案dtd
<!--
mvc4 configuration DTD.
Use the
<!DOCTYPE action PUBLIC "//DTD mvc4 Configuration//" "struts.dtd">
-->
<!ELEMENT struts (package)>
<!ELEMENT package (action*)>
<!ELEMENT action (result*)>
<!ATTLIST action
name CDATA #REQUIRED
class CDATA #REQUIRED
method CDATA #IMPLIED
>
<!ELEMENT result (#PCDATA|param)*>
<!ATTLIST result
name CDATA #IMPLIED
type CDATA #IMPLIED
>
<!ELEMENT param (#PCDATA)>
<!ATTLIST param
name CDATA #REQUIRED
>
14.xml檔案
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE action PUBLIC "//DTD mvc4 Configuration//" "struts.dtd">
<struts>
<package>
<action name="user_list" class="com.softeem.blog.action.UserAction" method="list">
<result type="forward">user_list.jsp</result>
</action>
</package>
</struts>
15.簡單檔案下載
response.setContentType("application/x-msdownload");//第一步設定檔案型別
String realpath = session.getServletContext().getRealPath("");
// String path=this.getInitParameter("path");
File file = new File(realpath + "/WEB-INF/files/asd.zip");
response.setContentLength((int)file.length());//第二步設定檔案長度
response.setHeader("Content-Disposition", "attachment;filename=asd.zip");//設定檔案頭
try {
InputStream in=new FileInputStream(file); //讀檔案
OutputStream out =response.getOutputStream();//寫檔案
byte[] buff=new byte[1024];
int len =0;
while((len=in.read(buff))!=-1){
out.write(buff,0,len);
}
out.close();
in.close();
16. 檔案上傳2
//123.jpg extname=.jpg 字尾名
try {
//先處理圖片
String path=session.getServletContext().getRealPath("");
//C:\Program Files\apache-tomcat-6.0.18\webapps\blog3
String extName=imgsrcFileName.substring(imgsrcFileName.indexOf("."));
String fileName = new Random().nextLong()+extName;
//12356132564465313.jpg 隨機生成的檔名
String pathFileName =path+"/imgs/"+fileName;
//檔案的絕對路徑C:\Program Files\apache-tomcat-6.0.18\webapps\imgs\12356132564465313.jpg
imgsrc.renameTo(new File(pathFileName));
//儲存檔名
user.setImgsrc("/imgs/"+fileName);
} catch (Exception e) {
e.printStackTrace();
}
//再儲存修改資料
Connection conn = DataSourceFactory.getDataSource().getConnection();
UserDAO userDAO = new UserDAO(conn);
userDAO.update(user);
conn.close();