学习笔记归纳 201095.1017

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();

原文地址:https://www.cnblogs.com/loveqin24/p/1998841.html