我刚学会了将file
上传到文件夹的地方。我也遵循了该教程,它也在工作。
当我提供路径C:\MyFile
时,它正在上传我的文件。
我正在使用Eclipse IDE,现在我在项目中创建了一个名为files
的文件夹。不幸的是,它没有上传,你可以看到一个截图我在哪里创建了一个文件夹来上传我的文件。
它不起作用你能帮助我吗?
我收到错误:
File not uploaded
以下是UploadFile.java
的源代码:
package com.fileupload;
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.catalina.ha.backend.Sender;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class UploadFile extends HttpServlet {
private static final long serialVersionUID = 1L;
private final String UPLOAD_DIRECTORY = "files/";
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
// process only if its multipart content
if (isMultipart) {
// Create a factory for disk-based file items
FileItemFactory factory = new DiskFileItemFactory();
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
try {
// Parse the request
List<FileItem> multiparts = upload.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));
}
}
}
catch (Exception e)
{
System.out.println("File not uploaded");
}
}
}
}
这是我的index.jsp
:
<!DOCTYPE html>
<html>
<head>
<title>Ajax File Upload with Progress Bar</title>
<!-- Include jQuery form & jQuery script file. -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.js" ></script>
<script src="http://malsup.github.com/jquery.form.js" ></script>
<script src="js/fileUploadScript.js" ></script>
<!-- Include css styles here -->
<link href="css/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h3>Ajax File Upload with Progress Bar</h3>
<form id="UploadForm" action="UploadFile" method="post"
enctype="multipart/form-data">
<input type="file" size="60" id="myfile" name="myfile"> <input
type="submit" value="Ajax File Upload">
<div id="progressbox">
<div id="progressbar"></div>
<div id="percent">0%</div>
</div>
<br />
<div id="message"></div>
</form>
</body>
</html>
记住
当我将路径UPLOAD_DIRECTORY
作为C:\Files
时。然后它正在工作..但是当我在我的eclipse项目中创建一个文件夹时(见上面我的截图)..它说的是File not uploaded
..
EDITED !!
现在我正在使用:
private final String UPLOAD_DIRECTORY =getServletContext().getRealPath("/files/");
但是得到错误:
SEVERE: Allocate exception for servlet UploadFile
java.lang.NullPointerException
at javax.servlet.GenericServlet.getServletContext(GenericServlet.java:125)
at com.fileupload.UploadFile.<init>(UploadFile.java:20)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:154)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1099)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:830)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:137)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:107)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:76)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:934)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:90)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:515)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1012)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:642)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1597)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1555)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
当然,帮助将不胜感激!!
答案 0 :(得分:0)
使用以下代码获取目录的真实路径:
getServletContext().getRealPath("/files/");
有关此方法的详细信息,请参阅BalusC's answer。
如果您使用init()
方法调用此方法,请在init方法的第一行(推荐)或使用以下代码调用super.init(config);
:
config.getServletContext().getRealPath("/files/");