修改bug

This commit is contained in:
2023-07-08 23:29:16 +08:00
parent fe739ea9e6
commit 2f1b8d646d
7 changed files with 47 additions and 31 deletions

View File

@@ -28,7 +28,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);

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

@@ -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,20 +15,23 @@ 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");
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");
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";
@@ -40,18 +44,18 @@ public class DownLoadServlet extends HttpServlet {
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);
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 +64,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 +76,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

@@ -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,19 @@ public class commitProjectServlet extends HttpServlet {
String projectUrl = req.getParameter("projectUrl");
String projectTitle = req.getParameter("projectTitle");
String projectDescription = req.getParameter("projectDescription");
String [] categoryIdList = req.getParameterValues("categoryIdList");
String categoryIdList = req.getParameter("categoryIdList");
String []categoryIdList1 = categoryIdList.split(",");
List<String> categoryIdList2 = Arrays.asList(categoryIdList1);
System.out.println(username);
System.out.println(projectName);
System.out.println(projectIco);
System.out.println(projectUrl);
System.out.println(projectTitle);
System.out.println(projectDescription);
System.out.println(categoryIdList);
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

@@ -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);
}