成功上载多个文件后拒绝访问

agyaoht7  于 2021-06-17  发布在  Mysql
关注(0)|答案(0)|浏览(153)

成功上载多个文件后拒绝访问。
这是我上载多个文件时遇到的错误:
naser.jpg naseer.new.pdf naseer.new.docx errorjava.io.ioexception:java.io.filenotfoundexception:d:\srinu(访问被拒绝)
其中naser.jpg naseer.new.pdf naseer.new.docx是我上传的文件名。
我找不到错误,有人能帮我找出错误吗。

Eduvationservletimp

import java.io.File;

import java.io.IOException;

import java.io.PrintWriter;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import javax.servlet.ServletException;

import javax.servlet.annotation.MultipartConfig;

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 javax.servlet.http.Part;

@WebServlet(urlPatterns = {"/UploadServlet"})

@MultipartConfig(fileSizeThreshold = 1024 * 1024 * 10, maxFileSize = 1024 * 1024 * 50, maxRequestSize = 1024 * 1024 * 100)

public class EducationServletimp extends HttpServlet {

    private static final String education = null;

// this if directory name where the file will be uploaded and saved

// this is the method which is created by system it self

protected void processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

try (PrintWriter out = response.getWriter()) {

// this tyr is created by me for the connection of database

try {

// this is the path provide by me to save the image

  //     HttpSession session=request.getSession(false);
  //  String uname=(String)session.getAttribute("uname");

    // HttpSession session=request.getSession(false);
  // String uname=(String)session.getAttribute("uname");

String savePath = "d:/srinu";
/*in place of C: you can place a path wher you need to save the image*/

// this comment will picup the image file and have convert it into file type

File fileSaveDir = new File(savePath);

if (!fileSaveDir.exists()) {

fileSaveDir.mkdirs();

}

// this two comment will take the name and image form web page

String sscboard =request.getParameter("sscboard");
String schoolname =request.getParameter("schoolname");
String percentagein =request.getParameter("percentagein");
String percentage =request.getParameter("percentage");
String passedout =request.getParameter("passedout");
String ssccountry =request.getParameter("ssccountry");
String sscstate =request.getParameter("sscstate");
String sscdistrict =request.getParameter("sscdistrict"); 
Part sscmemo = request.getPart("sscmemo");

String interboard =request.getParameter("interboard");
String intercollege =request.getParameter("intercollege");
String interpercentage =request.getParameter("interpercentage");
String interbranch =request.getParameter("interbranch");
String interpassedout =request.getParameter("interpassedout");
String intercountry =request.getParameter("intercountry");
String interstate =request.getParameter("interstate");
String interdistrict =request.getParameter("interdistrict"); 
Part intermemo = request.getPart("intermemo");

String degree =request.getParameter("degree");
String university =request.getParameter("university");
String collegename =request.getParameter("collegename");
String branch =request.getParameter("branch");
String degreepassedout =request.getParameter("degreepassedout");
String degreepercentage =request.getParameter("degreepercentage");
String degreecountry =request.getParameter("degreecountry");
String degreestate =request.getParameter("degreestate");
String degreedistrict =request.getParameter("degreedistrict"); 
Part cmm = request.getPart("cmm");

String pguniversity =request.getParameter("pguniversity");
String pgcollege =request.getParameter("pgcollege");
String pgbranch =request.getParameter("pgbranch");
String pgpassedout =request.getParameter("pgpassedout");
String pgpercentage =request.getParameter("pgpercentage");
String pgcountry =request.getParameter("pgcountry");
String pgstate =request.getParameter("pgstate");
String pgdistrict =request.getParameter("pgdistrict"); 
Part pgmemo = request.getPart("pgmemo");

// this comment will extract the file name of image

String fileName = extractFileName(sscmemo);
String fileName1 = extractFileName(intermemo);
String fileName2 = extractFileName(cmm);
String fileName3 = extractFileName(pgmemo);

// this will print the image name and user provide name

out.println(fileName);
out.println(fileName1);
out.println(fileName2);
out.println(fileName3);

//out.println("\n" + name);

/*if you may have more than one files with same name then you can calculate

some random characters and append that characters in fileName so that it will

make your each image name identical.*/

sscmemo.write(savePath + File.separator + fileName);
intermemo.write(savePath + File.separator + fileName1);
cmm.write(savePath + File.separator + fileName2);
pgmemo.write(savePath + File.separator + fileName3);

/*

You need this loop if you submitted more than one file

for (Part part : request.getParts()) {

String fileName = extractFileName(part);

part.write(savePath + File.separator + fileName);

}*/

// connectio to database

Class.forName("com.mysql.jdbc.Driver");

Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/verification","root","toor");

// query to insert name and image name

String query = "INSERT INTO education(sscmemo,intermemo,cmm,pgmemo) values(?,?,?,?)";

PreparedStatement pst;

pst = con.prepareStatement(query);

pst.setString(1, sscboard);
pst.setString(2, schoolname);
pst.setString(3, percentagein);
pst.setString(4, percentage);
pst.setString(5, passedout);
pst.setString(6, ssccountry);
pst.setString(7, sscstate);
pst.setString(8, sscdistrict); 
String filePath = savePath + File.separator + fileName;
pst.setString(1, filePath);

pst.setString(10, interboard);
pst.setString(11, intercollege);
pst.setString(12, interpercentage);
pst.setString(13, interbranch);
pst.setString(14, interpassedout);
pst.setString(15, intercountry);
pst.setString(16, interstate);
pst.setString(17, interdistrict); 
String filePath1 = savePath + File.separator + fileName1;
pst.setString(2, filePath1);

pst.setString(19, degree);
pst.setString(20, university);
pst.setString(21, collegename);
pst.setString(22, branch);
pst.setString(23, degreepassedout);
pst.setString(24, degreepercentage);
pst.setString(25, degreecountry);
pst.setString(26, degreestate);
pst.setString(27, degreedistrict);
String filePath2 = savePath + File.separator + fileName2;
pst.setString(3, filePath2);

pst.setString(29, pguniversity);
pst.setString(30, pgcollege);
pst.setString(31, pgbranch);
pst.setString(32, pgpassedout);
pst.setString(33, pgpercentage);
pst.setString(34, pgcountry);
pst.setString(35, pgstate);
pst.setString(36, pgdistrict);
String filePath3 = savePath + File.separator + fileName3;
pst.setString(4, filePath3);

int i=pst.executeUpdate();  
if(i>0) {
    out.print("<p>Record saved successfully!</p>");  
}else{  
    out.println("Sorry! unable to save record");  
} 

} catch (Exception ex) {

out.println("error" + ex);

}

}

}

// the extractFileName() is method used to extract the file name

private String extractFileName(Part part) {

String contentDisp = part.getHeader("content-disposition");

String[] items = contentDisp.split(";");

for (String s : items) {

if (s.trim().startsWith("filename")) {

return s.substring(s.indexOf("=") + 2, s.length() - 1);

}

}

return "";

}

@Override

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

processRequest(request, response);

}

@Override

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

processRequest(request, response);

}

@Override

public String getServletInfo() {

return "Short description";

}

}

请提供解决方案。谢谢您

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题