我正在制作简单的上载servlet。
我的代码是:
private final int FILE_LENGHT = 150;
private final String UPLOAD_DIRECTORY = "/img/";
.
.
.
.
.
@Override
public void uploadFile(HttpServletRequest request) {
//process only if its multipart content
if (ServletFileUpload.isMultipartContent(request)) {
try {
List<FileItem> multiparts = new ServletFileUpload(
new DiskFileItemFactory()).parseRequest(request);
for (FileItem item : multiparts) {
if (!item.isFormField()) {
File f = new File(item.getName());
String ex = suo.getFileExtension(f);
String name = suo.randomString(FILE_LENGHT) + "." + ex;
item.write(new File(UPLOAD_DIRECTORY + File.separator + name));
}
}
//File uploaded successfully
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");
}
}
我尝试将img上传到WEB-PAGES
文件夹。有可能的?
该文件夹的结构为:
Web Pages
WEB-INF
img
upload.jsp
但是我一直都很棒:
File Upload Failed due to java.io.FileNotFoundException:
The system can not find the path
它适用于路径的代码,C:\uploadFolder
但不适用于上传到Web Pages
文件夹。
为什么?
您想上传到Web应用程序中,以便您可以再次下载该文件吗?对于某些ServletEngine
servletRequest.getSession().getServletContext().getRealPath("/")
解析为“真实的”文件系统路径(如果应用程序部署爆炸等)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句