servlet+Mysql实现的校园论坛管理系统
本系统是一个简单的校园论坛系统,学生可以在线发帖并进行帖子评论回复,同同时管理员可以对用户进行管理。
(文末查看完整源码)
实现功能截图
excel上传
上传成功excel下载
系统功能
本系统实现了以下功能:
1、excel上传
2、excel下载
等
使用技术
数据库:mysql
开发工具:Idea(Myeclispe、Eclipse也可以)
知识点:SSH
项目结构
代码
java端
实体类
User.java
package com.spark.bean;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "t_user")
public class User implements Serializable {
/**
*
*/
private static final long serialVersionUID = -9042615274714038279L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Column(name="number")
private Integer number;
@Column(name = "name")
private String name;
@Column(name = "password")
private String password;
@Column(name = "run")
private double run;
/**
* @return the id
*/
public Integer getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(Integer id) {
this.id = id;
}
/**
* @return the number
*/
public Integer getNumber() {
return number;
}
/**
* @param number the number to set
*/
public void setNumber(Integer number) {
this.number = number;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
/**
* @return the run
*/
public double getRun() {
return run;
}
/**
* @param run the run to set
*/
public void setRun(double run) {
this.run = run;
}
}
FileUploadDaoImpl.java
package com.spark.dao.impl;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.spark.dao.FileUploadDao;
@Repository
public class FileUploadDaoImpl implements FileUploadDao{
@Autowired
private SessionFactory sessionFactory;
@Transactional
public void save(Object object) {
sessionFactory.getCurrentSession().save(object);
}
}
UploadController.java
package com.spark.controller;
import java.util.List;
import java.awt.Menu;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import com.spark.bean.User;
import com.spark.service.FileUploadService;
@Controller
public class UploadController {
@Resource(name="service")
private FileUploadService fileUploadService1;
@RequestMapping("/uploadfile")
public String toUpload(){
return "upload";
}
@RequestMapping("/form")
public String upload(@RequestParam(value="file",required=false) MultipartFile file,HttpServletRequest request,ModelMap model,Model mod) throws Exception{
String path=request.getSession().getServletContext().getRealPath("upload");
//文本路径
System.out.println(path);
String fileName=file.getOriginalFilename();
File targetFile=new File(path,fileName);
if(!targetFile.exists()){
targetFile.mkdirs();
}
// 获得上传文件的文件扩展名
String subname = fileName.substring(fileName.lastIndexOf(".")+1);
System.out.println("文件的扩展名:"+subname);
//保存
try{
file.transferTo(targetFile);
model.addAttribute("fileUrl",request.getSession().getServletContext().getRealPath("upload")+"/"+fileName);
}catch (Exception e) {
e.printStackTrace();
}
String rootpath = path + File.separator + fileName;
System.out.println(rootpath);
List<User> excelList = fileUploadService1.readExcel(rootpath);
for(User user : excelList){
fileUploadService1.save(user);
}
int len = excelList.size();
System.out.println("集合的大小为:"+len);
return "result";
}
}
完整源码
觉得有用,记得一键三连哦!