今天学长向大家分享一个 java web 毕设 项目:
文章发布系统的设计与实现
根据需求分析的结果,文章发布系统主要划分两个子模块后台管理模块,前台门户模块,各模块又包含子模块。分为以下几个模块
功能较多,学长这里就不一 一展示了
部分代码示例:
package control;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.RequestContext;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.servlet.ServletRequestContext;
import util.Info;
import dao.CommDAO;
public class MainCtrl extends HttpServlet {
public MainCtrl() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void go(String url,HttpServletRequest request, HttpServletResponse response)
{
try {
request.getRequestDispatcher(url).forward(request, response);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void gor(String url,HttpServletRequest request, HttpServletResponse response)
{
try {
response.sendRedirect(url);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String ac = request.getParameter("ac");
if(ac==null)ac="";
CommDAO dao = new CommDAO();
String date = Info.getDateStr();
String today = date.substring(0,10);
String tomonth = date.substring(0,7);
//登录
if(ac.equals("login"))
{
String username = request.getParameter("uname");
String password = request.getParameter("upass");
String utype = request.getParameter("utype");
String sql = "select * from sysuser where uname='"+username+"' and upass='"+password+"' and utype='"+utype+"'";
List<HashMap> userlist = dao.select(sql);
if(userlist.size()!=1)
{
request.setAttribute("error", "");
go("/index.jsp", request, response);
}else{
request.getSession(). setAttribute("user", userlist.get(0));
gor("/newspubs/admin/index.jsp", request, response);
}
}
if(ac.equals("uploaddoc"))
{
try {
String filename="";
request.setCharacterEncoding("utf-8");
RequestContext requestContext = new ServletRequestContext(request);
if(FileUpload.isMultipartContent(requestContext)){
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setRepository(new File(request.getRealPath("/upfile/")+"/"));
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(100*1024*1024);
List items = new ArrayList();
items = upload.parseRequest(request);
FileItem fileItem = (FileItem) items.get(0);
if(fileItem.getName()!=null && fileItem.getSize()!=0)
{
if(fileItem.getName()!=null && fileItem.getSize()!=0){
File fullFile = new File(fileItem.getName());
filename = Info.generalFileName(fullFile.getName());
File newFile = new File(request.getRealPath("/upfile/")+"/" + filename);
try {
fileItem.write(newFile);
} catch (Exception e) {
e.printStackTrace();
}
}else{
}
}
}
go("/js/uploaddoc.jsp?docname="+filename, request, response);
} catch (Exception e1) {
e1.printStackTrace();
}
}
//登录
if(ac.equals("flogin"))
{
String username = request.getParameter("uname");
String password = request.getParameter("upass");
String utype = request.getParameter("utype");
String sql = "select * from sysuser where uname='"+username+"' and upass='"+password+"' ";
List<HashMap> userlist = dao.select(sql);
if(userlist.size()!=1)
{
request.setAttribute("error", "");
go("/login.jsp", request, response);
}else{
request.getSession(). setAttribute("user", userlist.get(0));
gor("index.jsp", request, response);
}
}
//修改密码
if(ac.equals("uppass"))
{
String pass = request.getParameter("pass");
String id = request.getParameter("id");
String sql = "update sysuser set upass='"+pass+"' where id="+id;
dao.commOper(sql);
request.setAttribute("suc", "");
go("/admin/uppass.jsp", request, response);
}
if(ac.equals("adduser"))
{
String uname = request.getParameter("uname");
String upass = request.getParameter("upass");
String utype = request.getParameter("utype");
int i = dao.getInt("select count(*) from sysuser where uname='"+uname+"'");
if(i==0)
{
String sql = "insert into sysuser values('"+uname+"','"+upass+"','"+date+"','"+utype+"')";
dao.commOper(sql);
}
gor("/newspubs/admin/adduser.jsp?suc=suc",request,response);
}
if(ac.equals("upuser"))
{
String id = request.getParameter("id");
String upass = request.getParameter("upass");
String utype = request.getParameter("utype");
String sql = "update sysuser set upass='"+upass+"',utype='"+utype+"' where id="+id;
dao.commOper(sql);
go("/admin/upuser.jsp?suc=suc",request,response);
}
if(ac.equals("uploadimg"))
{
try {
String filename="";
request.setCharacterEncoding("utf-8");
RequestContext requestContext = new ServletRequestContext(request);
if(FileUpload.isMultipartContent(requestContext)){
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setRepository(new File(request.getRealPath("/upfile/")+"/"));
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(100*1024*1024);
List items = new ArrayList();
items = upload.parseRequest(request);
FileItem fileItem = (FileItem) items.get(0);
if(fileItem.getName()!=null && fileItem.getSize()!=0)
{
if(fileItem.getName()!=null && fileItem.getSize()!=0){
File fullFile = new File(fileItem.getName());
filename = Info.generalFileName(fullFile.getName());
File newFile = new File(request.getRealPath("/upfile/")+"/" + filename);
try {
fileItem.write(newFile);
} catch (Exception e) {
e.printStackTrace();
}
}else{
}
}
}
go("/js/uploadimg.jsp?filename="+filename, request, response);
} catch (Exception e1) {
e1.printStackTrace();
}
}
if(ac.equals("uploadimg"))
{
try {
String filename="";
request.setCharacterEncoding("utf-8");
RequestContext requestContext = new ServletRequestContext(request);
if(FileUpload.isMultipartContent(requestContext)){
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setRepository(new File(request.getRealPath("/upfile/")+"/"));
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(100*1024*1024);
List items = new ArrayList();
items = upload.parseRequest(request);
FileItem fileItem = (FileItem) items.get(0);
if(fileItem.getName()!=null && fileItem.getSize()!=0)
{
if(fileItem.getName()!=null && fileItem.getSize()!=0){
File fullFile = new File(fileItem.getName());
filename = Info.generalFileName(fullFile.getName());
File newFile = new File(request.getRealPath("/upfile/")+"/" + filename);
try {
fileItem.write(newFile);
} catch (Exception e) {
e.printStackTrace();
}
}else{
}
}
}
go("/js/uploadimg.jsp?filename="+filename, request, response);
} catch (Exception e1) {
e1.printStackTrace();
}
}
dao.close();
out.flush();
out.close();
}
public void init() throws ServletException {
// Put your code here
}
}
https://blog.csdn.net/mabile123444/article/details/119712695