0
jspをブートストラップで作成しました。 <form>
タグでは、ファイルをアップロードするためにサーブレットを呼び出していますが、はを呼び出して、part
オブジェクト(part.getName()
)のメソッドと呼び出しを試みたときにファイルを取得し続けているようです。
ご協力いただければ幸いです。 以下はコード全体です。
私が行方不明ですかを理解カント:サーブレットからファイルをアップロードできません
のweb.xml:
<servlet>
<servlet-name>Add New Candidate</servlet-name>
<servlet-class>AddCandidate</servlet-class>
<init-param>
<param-name>file-upload-internal</param-name>
<param-value>Resume/internal</param-value>
</init-param>
<init-param>
<param-name>file-upload-external</param-name>
<param-value>Resume/external</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Add New Candidate</servlet-name>
<url-pattern>/Add_Candidate</url-pattern>
</servlet-mapping>
JSP:
<div id="newProfileModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<form method="post" action="Add_Candidate" enctype="multipart/form-data">
<div class="form-group">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<div class="medium">
<h4 class="modal-title">Name Candidate</h4>
</div>
</div>
<div class="modal-body">
<label for="usr">Name:</label>
<input type="text" id="usr" class="form-control" name="NewCandName"></input>
<br/>
<label for="attachment">Attach CV</label>
<input id="attachment" name="NewCandCV" type="file" class="file-loading" />
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-default">Add</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</form>
</div>
</div>
サーブレット:あなたがあなたのサーブレットに注釈を付ける必要がある
public class AddCandidate extends HttpServlet{
private String intFilePath;
private String extFilePath;
public void init(){
intFilePath = getServletContext().getInitParameter("file-upload-internal");
extFilePath = getServletContext().getInitParameter("file-upload-external");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
String name = request.getParameter("NewCandName");
Part part = request.getPart("NewCandCV");
String CV = part.getName();
try
{
String path="";
if(profile.equals("TCS Internal"))
path = intFilePath;
else if(profile.equals("External"))
path = extFilePath;
// gets absolute path of the web application
String appPath = request.getServletContext().getRealPath("");
// constructs path of the directory to save uploaded file
String savePath = appPath + "/" + path;
// creates the save directory if it does not exists
File fileSaveDir = new File(savePath);
if (!fileSaveDir.exists()) {
fileSaveDir.mkdir();
}
String fileName = "CV_"+name+"_"+contact;
part.write(savePath + File.separator + fileName);
/* for (Part part : request.getParts()) {
tmp=part;
String fileName = "CV_"+name+"_"+contact;
part.write(savePath + File.separator + fileName);
} */
}
catch(Exception e){
writer.println("<html><head></head>"
+"<body>Exception!!"
+e+"</body></html>");
}
writer.println("<html><head></head>"
+"<body>Upload completed</body></html>");
}
}