Merge remote-tracking branch 'origin/master'

This commit is contained in:
cyk
2023-07-09 10:58:11 +08:00
15 changed files with 121 additions and 114 deletions

View File

@@ -64,7 +64,7 @@ public class UserDao {
connection = JdbcUtils.getConnection();
// sql语句写的操作 ----加上事务
JdbcUtils.beginTransaction(connection); // 开启事务
String insertSql = "INSERT INTO `user` (`userId`, `userName`, `userPassword`,`creatTime`,`isValid`,projectNum) VALUES (null, ?, ?,?,1,0);";
String insertSql = "INSERT INTO `user` (`userId`, `userName`, `userPassword`,`creatTime`,`isValid`,projectNum,contributionValue,userLevel,userAvatar) VALUES (null, ?, ?,?,1,0,0,1,'E://Desktop//helloGithub//web//uploadFile//69e56c15-20d5-43a4-916b-1d6230a33b8e_default.png');";
preparedStatement = connection.prepareStatement(insertSql);
preparedStatement.setString(1, userName);
preparedStatement.setString(2, userPwd);

View File

@@ -6,12 +6,10 @@ import com.hellogithub.entity.userEntity;
import com.hellogithub.utils.JdbcUtils;
import com.mysql.cj.jdbc.JdbcConnection;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.*;
import java.util.List;
public class githubDao {
@@ -28,7 +26,7 @@ public class githubDao {
PreparedStatement prep = conn.prepareStatement(sql1);
prep.setInt(1,projectId);
rs = prep.executeQuery();
if(rs == null){
if(!rs.next()){
connection = JdbcUtils.getConnection();
String Sql = "INSERT INTO githubinfo(id,projectId,avatar,name,starImgUrl,starCount,mainLanguage,isActive,subscriber,issues,isOrganization,defaultBranch,forks,protocol,version,isVaild) VALUES(null,?,?,?,?,?,?,?,?,?,?,?,?,?,?,0);";
preparedStatement = connection.prepareStatement(Sql);
@@ -62,18 +60,24 @@ public class githubDao {
return back;
}
public List<githubEntity> ret_Github_info(int projectid){
public ResultSetMetaData ret_Github_info(int projectid) {
List<githubEntity> githubEntityList;
ResultSetMetaData rsmd = null;
String sql = "select * from githubinfo WHERE projectId = ?;";
try {
Connection conn = JdbcUtils.getConnection();
QueryRunner runner = new QueryRunner();
String sql = "select * from githubinfo where projectId = ? and isValid = 1";
githubEntityList = runner.query(conn, sql, new BeanListHandler<>(githubEntity.class), projectid);
PreparedStatement prep = conn.prepareStatement(sql);
ResultSet rs = null;
prep.setInt(1, projectid);
rs = prep.executeQuery();
while (rs.next()) {
rsmd = rs.getMetaData();
}
conn.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
return githubEntityList;
return rsmd;
}
}

View File

@@ -14,7 +14,7 @@ public class projectCategoryDao {
try {
Connection conn = JdbcUtils.getConnection();
QueryRunner runner = new QueryRunner();
String sql ="INSERT into project_category (userId,projectId) VALUES (?,?)";
String sql ="INSERT into project_category (categoryId,projectId) VALUES (?,?)";
num = runner.update(conn, sql,userId,projectId);
conn.close();
} catch (SQLException e) {

View File

@@ -290,7 +290,7 @@ public class projectDao {
Connection conn = JdbcUtils.getConnection();
QueryRunner runner = new QueryRunner();
String sql ="update project set fileAddress = ? where projectName = ? and isValid = 1";
num = runner.update(conn, sql,projectName,fileAddress);
num = runner.update(conn, sql,fileAddress,projectName);
conn.close();
} catch (SQLException e) {
throw new RuntimeException(e);
@@ -320,7 +320,7 @@ public class projectDao {
PreparedStatement prep = conn.prepareStatement(sql);
prep.setString(1, projectName);
rs = prep.executeQuery();
if(rs.wasNull())
if(rs.next())
return 1;
else
return 0;

View File

@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONObject;
import com.hellogithub.dao.githubDao;
import com.hellogithub.entity.githubEntity;
import java.sql.ResultSetMetaData;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -16,12 +17,7 @@ public class githubService {
}
public String ret_Github_info(int projectId){
List<githubEntity> githubEntityList = githubDao.ret_Github_info(projectId);
Map<Integer, githubEntity> dataMap = new HashMap<>();
for(int i=1;i<githubEntityList.size()+1;i++){
githubEntity githubEntity = githubEntityList.get(i);
dataMap.put(i,githubEntity);
}
return JSONObject.toJSONString(dataMap);
ResultSetMetaData rsmd = githubDao.ret_Github_info(projectId);
return JSONObject.toJSONString(rsmd);
}
}

View File

@@ -252,22 +252,23 @@ public class projectService {
String submitTime = format.format(time).toString();
int categoryId = categoryIdList.get(0);
int userId = userDao.selectIdByName(username);
for(int i=0;i<categoryIdList.size();i++)
{
int id = categoryIdList.get(i);
int num = projectCategoryDao.insertProject(userId,id);
if(num == 0 )
{
flag=0;
break;
}
}
pnum = projectDao.insertProject(userId,projectName,projectUrl,projectIco,projectTitle,projectDescription,submitTime,categoryId);
if(pnum == 0 )
{
flag=0;
}
int proId = projectDao.selectProByProName(projectName).getProjectId();
for(int i=0;i<categoryIdList.size();i++)
{
int id = categoryIdList.get(i);
int num = projectCategoryDao.insertProject(id,proId);
if(num == 0 )
{
flag=0;
break;
}
}
return flag ;
}

View File

@@ -1,6 +1,7 @@
package com.hellogithub.servlet;
import com.alibaba.fastjson.JSONObject;
import com.hellogithub.service.projectService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
@@ -14,44 +15,44 @@ 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 fileName = request.getParameter("filename");
System.out.println(fileName);
String projectId = request.getParameter("id");
String address = projectService.selectProById(projectId).getFileAddress();
String[] liss = address.split("\\\\");
String fileName = liss[liss.length - 1];
//本地需要进行转码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");
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);
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);
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);
FileInputStream in = new FileInputStream(fileSaveRootPath + File.separator + fileName);
//创建输出流
OutputStream out = response.getOutputStream();
@@ -60,7 +61,7 @@ public class DownLoadServlet extends HttpServlet {
byte buffer[] = new byte[1024];
int len = 100;
//循环将输入流中的内容读取到缓冲区当中
while((len=in.read(buffer))>0){
while ((len = in.read(buffer)) > 0) {
//输出缓冲区的内容到浏览器,实现文件下载
out.write(buffer, 0, len);
}
@@ -72,21 +73,21 @@ public class DownLoadServlet extends HttpServlet {
}
/**
* @param filename 要下载的文件名
* @param saveRootPath 上传文件保存的根目录,也就是/WEB-INF/upload目录
* @return 要下载的文件的存储目录
* @Method: findFileSavePathByFileName
* @Description: 通过文件名和存储上传文件根目录找出要下载的文件的所在路径
* @Anthor:
* @param filename 要下载的文件名
* @param saveRootPath 上传文件保存的根目录,也就是/WEB-INF/upload目录
* @return 要下载的文件的存储目录
*/
public String findFileSavePathByFileName(String filename,String saveRootPath){
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()){
if (!file.exists()) {
//创建目录
file.mkdirs();
}

View File

@@ -45,6 +45,10 @@ public class UploadHandleServlet extends HttpServlet {
String savePath = this.getServletContext().getRealPath(File.separator+"WEB-INF"+File.separator+"upload");
savePath= this.getServletContext().getRealPath(File.separator+"WEB-INF"+File.separator+"upload").replace("\\out\\artifacts\\helloGithub_war_exploded\\WEB-INF\\upload","\\web\\uploadFile");
System.out.println(savePath);
String needPath = "\\helloGithub\\web\\uploadFile";
String relPath = savePath.substring(savePath.length()-needPath.length(),savePath.length());
System.out.println(relPath);
//上传时生成的临时文件保存目录
String tempPath = this.getServletContext().getRealPath(File.separator+"WEB-INF"+File.separator+"tmp");
File tmpFile = new File(tempPath);
@@ -164,7 +168,7 @@ public class UploadHandleServlet extends HttpServlet {
message = "文件上传成功!";
if(userName != null)
{
userService.updateUserAv(userName,realSavePath+"\\"+saveFilename);
userService.updateUserAv(userName,relPath+"\\"+saveFilename);
System.out.println("123");
}

View File

@@ -46,7 +46,8 @@ public class commitProjectFileServlet extends HttpServlet {
//得到上传文件的保存目录将上传的文件存放于WEB-INF目录下不允许外界直接访问保证上传文件的安全
String savePath = this.getServletContext().getRealPath(File.separator+"WEB-INF"+File.separator+"upload");
savePath= this.getServletContext().getRealPath(File.separator+"WEB-INF"+File.separator+"upload").replace("\\out\\artifacts\\helloGithub_war_exploded\\WEB-INF\\upload","\\web\\projectFile");
System.out.println(savePath);
String needPath = "\\helloGithub\\web\\uploadFile";
String relPath = savePath.substring(savePath.length()-needPath.length(),savePath.length());
//上传时生成的临时文件保存目录
String tempPath = this.getServletContext().getRealPath(File.separator+"WEB-INF"+File.separator+"tmp");
File tmpFile = new File(tempPath);
@@ -164,7 +165,7 @@ public class commitProjectFileServlet extends HttpServlet {
//删除处理文件上传时生成的临时文件
item.delete();
message = "文件上传成功!";
num = projectService.updateProject(projectName,realSavePath+"\\"+saveFilename);
num = projectService.updateProject(projectName,relPath+"\\"+saveFilename);
}
}

View File

@@ -13,6 +13,7 @@ import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
@WebServlet("/commitProject")
@@ -52,9 +53,12 @@ public class commitProjectServlet extends HttpServlet {
String projectUrl = req.getParameter("projectUrl");
String projectTitle = req.getParameter("projectTitle");
String projectDescription = req.getParameter("projectDescription");
String [] categoryIdList = req.getParameterValues("projectDescription");
String categoryIdList = req.getParameter("categoryIdList");
String []categoryIdList1 = categoryIdList.split(",");
List<String> categoryIdList2 = Arrays.asList(categoryIdList1);
List<Integer> integers = new ArrayList<>();
for (String s : categoryIdList) {
for (String s : categoryIdList2) {
integers.add(Integer.parseInt(s));
}
int flag = projectService.insertProject(username,projectName,projectUrl,projectIco,projectTitle,projectDescription,integers);

View File

@@ -12,7 +12,7 @@ import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
@WebServlet("returnGithubInfo")
@WebServlet("/returnGithubInfo")
public class ret_Github_infoServlet extends HttpServlet {
private githubService githubservice = new githubService();
@Override
@@ -35,8 +35,7 @@ public class ret_Github_infoServlet extends HttpServlet {
PrintWriter writer = resp.getWriter();
String projectid = req.getParameter("projectId");
String jsonString = githubservice.ret_Github_info(Integer.parseInt(projectid));
writer.println(jsonString);
writer.println(githubservice.ret_Github_info(Integer.parseInt(projectid)));
writer.close();
}
}

View File

@@ -17,10 +17,6 @@ import java.nio.channels.Channels;
@WebServlet("/retUserAv")
public class retUserAvServlet extends HttpServlet {
private UserService userService = new UserService();
@Override
public void init(ServletConfig config) throws ServletException {
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
@@ -36,12 +32,14 @@ public class retUserAvServlet extends HttpServlet {
userEntity=userService.selectUserByName(username);
if(userEntity != null)
{
String savePath = this.getServletContext().getRealPath(File.separator+"WEB-INF"+File.separator+"upload");
savePath= this.getServletContext().getRealPath(File.separator+"WEB-INF"+File.separator+"upload").replace("\\out\\artifacts\\helloGithub_war_exploded\\WEB-INF\\upload","\\web\\projectFile");
String needPath = "\\helloGithub\\web\\uploadFile";
String relPath = savePath.substring(0,savePath.length()-needPath.length());
String uri = userEntity.getUserAvatar();
if(!uri.startsWith("https"))
{
FileInputStream fis = new FileInputStream (uri);
relPath = relPath+uri;
FileInputStream fis = new FileInputStream (relPath);
String type = userEntity.getUserAvatar().substring(userEntity.getUserAvatar().lastIndexOf(".")+1);
int size = fis.available();
if(size != 0)
{
@@ -54,21 +52,7 @@ public class retUserAvServlet extends HttpServlet {
os.write(data);
os.flush();
os.close();
}
}else{
PrintWriter writer = resp.getWriter();
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html; charset=utf-8");
// 设置响应头允许ajax跨域访问
String curOrigin = req.getHeader("Origin");
resp.setHeader("Access-Control-Allow-Origin", curOrigin == null ? "true" : curOrigin);
resp.setHeader("Access-Control-Allow-Credentials", "true");
resp.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, HEAD");
resp.setHeader("Access-Control-Max-Age", "3600");
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");
writer.println(uri);
writer.close();
}

View File

@@ -2,10 +2,10 @@ package com.hellogithub.servlet;
import com.alibaba.fastjson.JSONObject;
import com.hellogithub.service.projectService;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
@@ -35,7 +35,7 @@ public class selectProjectByPname extends HttpServlet {
PrintWriter writer = resp.getWriter();
String projectName = req.getParameter("projectName");
int bool = projectservice.selectProjectByPname("projectName");
int bool = projectservice.selectProjectByPname(projectName);
if(bool == 1){
setResultError("项目已存在", writer);
}