1.使用JAR
jsp文件上传主要使用了两个jar包,commons-fileupload-1.2.1.jar和commons-io-1.4.jar
2.代码实现
public class UploadServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
private ServletContext sc;
private String savePath;
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
System.out.println("请求进来了..........");
// 设置请求的编码
request.setCharacterEncoding("UTF-8");
DiskFileItemFactory factory = new DiskFileItemFactory();//创建一个磁盘文件工厂
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List items = upload.parseRequest(request);
Iterator it = items.iterator();
while (it.hasNext()) {
FileItem item = (FileItem) it.next();
if (item.isFormField()) {
System.out.println("表单的参数名称:" + item.getFieldName()
+ ",对应的参数值:" + item.getString("UTF-8"));
} else {
// 获取文件扩展名
String strtype = item.getName().substring(
item.getName().length() - 3,
item.getName().length());
strtype = strtype.toLowerCase();
if (strtype == "jpg" || strtype == "gif"
|| strtype == "txt") {
if (item.getName() != null
&& !item.getName().equals("")) {
System.out.println("上传文件的大小:" + item.getSize());
System.out.println("上传文件的类型:"
+ item.getContentType());
System.out.println("上传文件的名称:" + item.getName());
System.out.println("文件的扩展名" + strtype);
File tempFile = new File(item.getName());
File file = new File(
sc.getRealPath("/") + savePath, tempFile
.getName());
item.write(file);
request.setAttribute("upload.message", "上传文件成功!");
} else {
request.setAttribute("upload.message",
"没有选择上传文件获取格式不支持");
}
} else {
request.setAttribute("upload.message", "上传文件格式不支持");
}
}
}
} catch (Exception e) {
e.printStackTrace();
request.setAttribute("upload.message", "上传文件不成功!");
}
// 转发
request.getRequestDispatcher("/uploadResult.jsp").forward(request,
response);
}
@Override
public void init(ServletConfig config) throws ServletException {
savePath = config.getInitParameter("savePath");
sc = config.getServletContext();
}
1.关联映射的本质:就是将关联关系映射到数据库中,关联关系指对象模型中的一个或多个引用.
2.下面列举多对一的示例:用户和组(多个用户属于一个组)多对一关联映射是最常用的一种关联映射
*User 类
package com.lzy
public class User{
private int id;
private String name;
private Group group;//持有组的引用
public User(){};
//省略set,get方法
}
*Group类
package com.lzy
public class Group{
private int id;
private String name;
//省略get,set方法
}
3.对对象进行关系映射,这也是Hibernate中比较难的一点。
(1)User.hbm.xml
<?xml version="1.0">
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" http://hibernate.sourceforge.net/hibernate-mapping-3.0
.dtd">
<hibernate-mapping package="com.lzy">
<class name="User" table="t_user">
<id name="id" column="id">
<genarator class="native"/>
</id>
<property name="name" column="user_name" not-null="true"/>
<many-to-one name="group" column="groupid"/>
</calss>
</hibernate-mapping>
(2)Group.hbm.xml
<?xml version="1.0">
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" http://hibernate.sourceforge.net/hibernate-mapping-3.0
.dtd">
<hibernate-mapping package="com.lzy">
<class name="Group" table="t_group">
<id name="id" column="id">
<genarator class="native"/>
</id>
<property name="name" column="group_name" not-null="true"/>
</class>
</hibernate-mapping>
4.测试
public class Test {
public static void main(String args[]){
SessionFactory sessionFactory=null;
Session session=null;
Transaction transaction=null;
sessionFactory = HibernateUtil.getSessionFactory();// 创建一个会话工厂
session = sessionFactory.openSession();// 创建一个会话实例
transaction = session.beginTransaction();// 申明一个事务
User user= new User();
Group group = new Group();
user.setName("龙一");
group.setName("中南大学");
user.setGroup(group);
try {
transaction.begin();
session.save(user);
transaction.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
}