优化
This commit is contained in:
@@ -308,7 +308,7 @@ public class AdminDao {
|
||||
try {
|
||||
Connection conn = JdbcUtils.getConnection();
|
||||
QueryRunner runner = new QueryRunner();
|
||||
String sql="UPDATE article SET isValid = ? WHERE id = ?";
|
||||
String sql="UPDATE onefile SET isValid = ? WHERE id = ?";
|
||||
num = runner.update(conn, sql, isvalid,id);
|
||||
conn.close();
|
||||
} catch (SQLException e) {
|
||||
|
@@ -200,7 +200,7 @@ public class UserDao {
|
||||
*
|
||||
* 返回用户总数
|
||||
* */
|
||||
public long queryUserCount(){
|
||||
public userEntity queryUserCount(){
|
||||
userEntity userEntity;
|
||||
long num;
|
||||
try {
|
||||
@@ -214,7 +214,7 @@ public class UserDao {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
return num;
|
||||
return userEntity;
|
||||
}
|
||||
|
||||
|
||||
|
@@ -86,7 +86,7 @@ public class categoryDao {
|
||||
try {
|
||||
Connection conn = JdbcUtils.getConnection();
|
||||
QueryRunner runner = new QueryRunner();
|
||||
String sql="delete from category where articleId = ?";
|
||||
String sql="delete from category where categoryId = ?";
|
||||
num = runner.update(conn,sql,id);
|
||||
conn.close();
|
||||
} catch (SQLException e) {
|
||||
|
@@ -21,7 +21,7 @@ public class fileDao {
|
||||
try {
|
||||
Connection conn = JdbcUtils.getConnection();
|
||||
QueryRunner runner = new QueryRunner();
|
||||
String sql="select * from onefile ";
|
||||
String sql="select * from onefile where isValid = 1";
|
||||
fileEntityList = runner.query(conn,sql,new BeanListHandler<>(fileEntity.class));
|
||||
conn.close();
|
||||
} catch (SQLException e) {
|
||||
@@ -80,8 +80,8 @@ public class fileDao {
|
||||
try {
|
||||
Connection conn = JdbcUtils.getConnection();
|
||||
QueryRunner runner = new QueryRunner();
|
||||
String sql="update onefile set oneFileName = ? , language = ? ,description =?, userId=?, url=?, lookNum=?,Content=?,isValid=? where id =? ";
|
||||
num = runner.update(conn,sql, oneFileName ,language ,description,userId, url, lookNum ,contextFile ,isValid,fileId);
|
||||
String sql="update onefile set oneFileName = ? , language = ? ,description =?, userId=?, url=?, lookNum=?,isValid=? where id =? ";
|
||||
num = runner.update(conn,sql, oneFileName ,language ,description,userId, url, lookNum ,isValid,fileId);
|
||||
conn.close();
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
@@ -110,7 +110,7 @@ public class fileDao {
|
||||
try{
|
||||
Connection conn = JdbcUtils.getConnection();
|
||||
QueryRunner runner = new QueryRunner();
|
||||
String sql = "INSERT INTO onefile(oneFileName,language,description,userId,url,lookNum,Content,isValid) VALUES(?,?,?,?,?,0,?,1);";
|
||||
String sql = "INSERT INTO onefile(oneFileName,language,description,userId,url,lookNum,Content,isValid) VALUES(?,?,?,?,?,0,?,0);";
|
||||
int bool = 0;
|
||||
num = runner.update(conn, sql,oneFileName,language,description,userId,url,contextFile);
|
||||
conn.close();
|
||||
|
@@ -29,13 +29,13 @@ public class projectCategoryEntity {
|
||||
this.projectId = projectId;
|
||||
}
|
||||
|
||||
int num ;
|
||||
long num ;
|
||||
|
||||
public int getNum() {
|
||||
public long getNum() {
|
||||
return num;
|
||||
}
|
||||
|
||||
public void setNum(int num) {
|
||||
public void setNum(long num) {
|
||||
this.num = num;
|
||||
}
|
||||
String categoryName;
|
||||
|
@@ -62,7 +62,10 @@ public class UserService {
|
||||
* @param name
|
||||
* */
|
||||
public int dedleteAllLabel(String name){return userDao.dedleteAllLabel(name);}
|
||||
public long queryUserCount(){return userDao.queryUserCount();}
|
||||
public long queryUserCount(){
|
||||
userEntity userEntity = userDao.queryUserCount();
|
||||
return userEntity.getNum();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据用户名返回贡献值
|
||||
|
@@ -52,8 +52,9 @@ public class oneFileService {
|
||||
return fileDao.selectAllFile();
|
||||
}
|
||||
|
||||
public int insertOneFile(int userId ,String oneFileName, String language,String description ,
|
||||
public int insertOneFile(String username ,String oneFileName, String language,String description ,
|
||||
String url,String contextFile){
|
||||
int userId = userDao.selectUserByName(username).getUserId();
|
||||
return fileDao.insertOneFile(userId,oneFileName,language,description,url,contextFile);
|
||||
}
|
||||
}
|
||||
|
121
src/com/hellogithub/servlet/DownLoadServlet.java
Normal file
121
src/com/hellogithub/servlet/DownLoadServlet.java
Normal file
@@ -0,0 +1,121 @@
|
||||
package com.hellogithub.servlet;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.hellogithub.service.projectService;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.*;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.HashMap;
|
||||
import java.util.UUID;
|
||||
|
||||
@WebServlet("/DownLoad")
|
||||
public class DownLoadServlet extends HttpServlet {
|
||||
private projectService projectService = new projectService();
|
||||
|
||||
public void doGet(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException {
|
||||
response.reset();
|
||||
response.setContentType("application/x-msdownload;charset=utf-8");
|
||||
//得到要下载的文件名
|
||||
String projectId = request.getParameter("id");
|
||||
String address = projectService.selectProById(projectId).getFileAddress();
|
||||
String[] liss = address.split("\\\\");
|
||||
String fileName = liss[liss.length - 1];
|
||||
System.out.println(fileName);
|
||||
//本地需要进行转码,linux服务器使用就会出现下载乱码
|
||||
//fileName = new String(fileName.getBytes("iso8859-1"),"UTF-8");
|
||||
// System.out.print("转码后"+fileName);
|
||||
//上传的文件都是保存在/WEB-INF/upload目录下的子目录当中
|
||||
String fileSaveRootPath = this.getServletContext().getRealPath(File.separator+"WEB-INF"+File.separator+"upload").replace("\\out\\artifacts\\helloGithub_war_exploded\\WEB-INF\\upload","\\web\\projectFile");;
|
||||
//通过文件名找出文件的所在目录
|
||||
//String path = findFileSavePathByFileName(fileName,fileSaveRootPath);
|
||||
// String path = "/WEB-INF/upload";
|
||||
//得到要下载的文件
|
||||
File file = new File(fileSaveRootPath + File.separator + fileName);
|
||||
//File file = new File(fileName);
|
||||
System.out.println(file);
|
||||
System.out.print("\n");
|
||||
// 浏览器请求响应转码防止乱码
|
||||
response.setCharacterEncoding("UTF-8");
|
||||
request.setCharacterEncoding("UTF-8");
|
||||
//如果文件不存在
|
||||
if (!file.exists()) {
|
||||
request.setAttribute("message", "wrong");
|
||||
request.getRequestDispatcher("/message.jsp").forward(request, response);
|
||||
return;
|
||||
}
|
||||
//处理文件名
|
||||
String realname = fileName.substring(fileName.indexOf("_") + 1);
|
||||
// System.out.println(realname);
|
||||
//设置响应头,控制浏览器下载该文件
|
||||
response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
|
||||
//读取要下载的文件,保存到文件输入流
|
||||
FileInputStream in = new FileInputStream(fileSaveRootPath + File.separator + fileName);
|
||||
|
||||
//创建输出流
|
||||
OutputStream out = response.getOutputStream();
|
||||
|
||||
//创建缓冲区
|
||||
byte buffer[] = new byte[1024];
|
||||
int len = 100;
|
||||
//循环将输入流中的内容读取到缓冲区当中
|
||||
while ((len = in.read(buffer)) > 0) {
|
||||
//输出缓冲区的内容到浏览器,实现文件下载
|
||||
out.write(buffer, 0, len);
|
||||
}
|
||||
//关闭文件输入流
|
||||
in.close();
|
||||
//关闭输出流
|
||||
out.close();
|
||||
// setResultOK("下载成功",writer);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param filename 要下载的文件名
|
||||
* @param saveRootPath 上传文件保存的根目录,也就是/WEB-INF/upload目录
|
||||
* @return 要下载的文件的存储目录
|
||||
* @Method: findFileSavePathByFileName
|
||||
* @Description: 通过文件名和存储上传文件根目录找出要下载的文件的所在路径
|
||||
* @Anthor:
|
||||
*/
|
||||
public String findFileSavePathByFileName(String filename, String saveRootPath) {
|
||||
// int hashcode = filename.hashCode();
|
||||
// int dir1 = hashcode&0xf; //0--15
|
||||
// int dir2 = (hashcode&0xf0)>>4; //0-15
|
||||
//String dir = saveRootPath + "\\" + dir1 + "\\" + dir2; //upload\2\3 upload\3\5
|
||||
String dir = saveRootPath;
|
||||
File file = new File(dir);
|
||||
if (!file.exists()) {
|
||||
//创建目录
|
||||
file.mkdirs();
|
||||
}
|
||||
return dir;
|
||||
}
|
||||
|
||||
public void doPost(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException {
|
||||
doGet(request, response);
|
||||
}
|
||||
|
||||
public void setResult(Integer code, String msg, PrintWriter writer) {
|
||||
HashMap<String, Object> result = new HashMap<>();
|
||||
result.put("code", code);
|
||||
result.put("msg", msg);
|
||||
String jsonString = JSONObject.toJSONString(result);
|
||||
writer.println(jsonString);
|
||||
writer.close();
|
||||
}
|
||||
|
||||
public void setResultError(String msg, PrintWriter writer) {
|
||||
setResult(500, msg, writer);
|
||||
}
|
||||
|
||||
public void setResultOK(String msg, PrintWriter writer) {
|
||||
setResult(200, msg, writer);
|
||||
}
|
||||
}
|
@@ -38,11 +38,11 @@ public class insertFileByAdminServlet extends HttpServlet {
|
||||
resp.setHeader("Access-Control-Allow-Headers", "access-control-allow-origin, authority, content-type, version-info, X-Requested-With");
|
||||
resp.setContentType("application/json;charset=UTF-8");
|
||||
|
||||
|
||||
//插入文件
|
||||
String oneFileName=req.getParameter("oneFileName");
|
||||
String language=req.getParameter("language");
|
||||
String description=req.getParameter("description");
|
||||
int userId= Integer.parseInt(req.getParameter("userId"));
|
||||
String Content=req.getParameter("Content");
|
||||
String url=req.getParameter("url");
|
||||
|
||||
@@ -53,7 +53,7 @@ public class insertFileByAdminServlet extends HttpServlet {
|
||||
setResultError("用户未登录",writer);
|
||||
}else{
|
||||
String username=userEntity.getUserName();
|
||||
int num = oneFileService.insertOneFile(userId,oneFileName,language,description,url,Content);
|
||||
int num = oneFileService.insertOneFile(username,oneFileName,language,description,url,Content);
|
||||
if(num == 0){
|
||||
setResultError("添加失败",writer);
|
||||
}else{
|
||||
|
@@ -94,6 +94,7 @@ public class AdminLoginServlet extends HttpServlet {
|
||||
public void setResult(Integer code, String msg, PrintWriter writer) {
|
||||
HashMap<String, Object> result = new HashMap<>();
|
||||
result.put("msg", msg);
|
||||
result.put("code", code);
|
||||
String jsonString = JSONObject.toJSONString(result);
|
||||
writer.println(jsonString);
|
||||
writer.close();
|
||||
|
@@ -1,5 +1,6 @@
|
||||
package com.hellogithub.servlet.systemAdmin;
|
||||
|
||||
import com.hellogithub.entity.adminEntity;
|
||||
import com.hellogithub.service.projectService;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
@@ -7,6 +8,7 @@ import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
@WebServlet("/retProjectByTime")
|
||||
@@ -30,6 +32,11 @@ public class retProjectByTimeServlet extends HttpServlet {
|
||||
resp.setHeader("Access-Control-Allow-Headers", "access-control-allow-origin, authority, content-type, version-info, X-Requested-With");
|
||||
resp.setContentType("application/json;charset=UTF-8");
|
||||
|
||||
HttpSession session = req.getSession();
|
||||
adminEntity user = (adminEntity)session.getAttribute("admin");
|
||||
if(user == null) {
|
||||
return;
|
||||
}
|
||||
String str = projectService.retProjectByTime();
|
||||
PrintWriter writer = resp.getWriter();
|
||||
writer.println(str);
|
||||
|
@@ -1,5 +1,6 @@
|
||||
package com.hellogithub.servlet.systemAdmin;
|
||||
|
||||
import com.hellogithub.entity.adminEntity;
|
||||
import com.hellogithub.service.categoryService;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
@@ -7,6 +8,7 @@ import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
@WebServlet("/retProjectNumByCategory")
|
||||
@@ -30,6 +32,11 @@ public class retProjectNumByCategoryServlet extends HttpServlet {
|
||||
resp.setHeader("Access-Control-Allow-Headers", "access-control-allow-origin, authority, content-type, version-info, X-Requested-With");
|
||||
resp.setContentType("application/json;charset=UTF-8");
|
||||
|
||||
HttpSession session = req.getSession();
|
||||
adminEntity user = (adminEntity)session.getAttribute("admin");
|
||||
if(user == null) {
|
||||
return;
|
||||
}
|
||||
String str = categoryService.retProjectNumByCategory();
|
||||
PrintWriter writer = resp.getWriter();
|
||||
writer.println(str);
|
||||
|
@@ -46,9 +46,9 @@ public class updateFileByAdminServlet extends HttpServlet {
|
||||
String oneFileName=req.getParameter("oneFileName");
|
||||
String language=req.getParameter("language");
|
||||
String description =req.getParameter("description");
|
||||
int isValid = Integer.parseInt(req.getParameter("isValid "));
|
||||
int isValid = Integer.parseInt(req.getParameter("isValid"));
|
||||
String url=req.getParameter("url");
|
||||
String Content=req.getParameter("Content");
|
||||
String Content="";
|
||||
int bool = oneFileService.updateFileDetail(fileId,userId,lookNum,oneFileName,language,description,isValid,url,Content);
|
||||
if(bool == 1){
|
||||
setResultOk("更新成功",writer);
|
||||
|
Reference in New Issue
Block a user