为什么我上传的文件名是空的? jsp/servlet
Why is the name of the file I upload null? jsp/servlet
Ι 使用以下 servlet 上传照片并且它有效。但是当我在 jsp 页面中打印照片名称时,它是空的。我需要它以便将它保存在我的数据库中。有什么想法吗?
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class FileUploadHandler extends HttpServlet {
private final String UPLOAD_DIRECTORY = "C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\ismgroup9\fileUpload";
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
if(ServletFileUpload.isMultipartContent(request)){
try {
List<FileItem> multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
for(FileItem item : multiparts){
if(!item.isFormField()){
String name = new File(item.getName()).getName();
item.write( new File(UPLOAD_DIRECTORY + File.separator + name));
request.setAttribute("photoname", name);
}
}
request.setAttribute("message", "File Uploaded Successfully");
} catch (Exception ex) {
request.setAttribute("message", "File Upload Failed due to " + ex);
}
}else{
request.setAttribute("message", "Sorry this Servlet only handles file upload request");
}
request.getRequestDispatcher("/uploadfileController.jsp").forward(request, response);
}
}
根据您的评论,您似乎使用了
request.getAttribute("photoName");
// ^here is the problem
但您的属性设置名称为 photoname
。
request.setAttribute("photoname", name);
Java 区分大小写,因此 photoname
和 photoName
不是相同的名称。始终仔细检查您的变量名称。
Ι 使用以下 servlet 上传照片并且它有效。但是当我在 jsp 页面中打印照片名称时,它是空的。我需要它以便将它保存在我的数据库中。有什么想法吗?
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class FileUploadHandler extends HttpServlet {
private final String UPLOAD_DIRECTORY = "C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\ismgroup9\fileUpload";
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
if(ServletFileUpload.isMultipartContent(request)){
try {
List<FileItem> multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
for(FileItem item : multiparts){
if(!item.isFormField()){
String name = new File(item.getName()).getName();
item.write( new File(UPLOAD_DIRECTORY + File.separator + name));
request.setAttribute("photoname", name);
}
}
request.setAttribute("message", "File Uploaded Successfully");
} catch (Exception ex) {
request.setAttribute("message", "File Upload Failed due to " + ex);
}
}else{
request.setAttribute("message", "Sorry this Servlet only handles file upload request");
}
request.getRequestDispatcher("/uploadfileController.jsp").forward(request, response);
}
}
根据您的评论,您似乎使用了
request.getAttribute("photoName");
// ^here is the problem
但您的属性设置名称为 photoname
。
request.setAttribute("photoname", name);
Java 区分大小写,因此 photoname
和 photoName
不是相同的名称。始终仔细检查您的变量名称。