我使用ServletFileUpload
上传图片
如下面的html:
<body>
<form id="formId" >
<input type="file">
<input type="submit" id="btn" value="submit">
</form>
<script>
$(document).ready(function(){
$("#btn").ready(function(){
data1= new FormData($("#formId")[0]);
$.ajax( {
url: 'Sample1',
type: 'POST',
data :data1,
processData: false,
contentType: false,
dataType:"text",
success:function(response)
{
}
} );
} );
});
</script>
</body>
我的servlet代码
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
System.out.println("entered in to servlet");
ServletFileUpload sf=new ServletFileUpload(new DiskFileItemFactory());
try {
List<FileItem> multifiles=sf.parseRequest(request);
for(FileItem item:multifiles){
item.write(new File("D:\\Projects\\i_seva\\WebContent\\"+item.getName()));
String ss= item.getName();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
当我点击提交按钮时,请求servlet
并打印消息entered in to servlet
,但文件未上传。
我无法追踪它是否是html或servlet的错误请帮助我。
通过添加name="file"
<input type="file" name="file">
但新的异常发生如下
java.io.FileNotFoundException: D:\Projects\i_seva\WebContent (Access is denied)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at org.apache.commons.fileupload.disk.DiskFileItem.write(DiskFileItem.java:394)
at com.iseva.Servlet.Sample1.doPost(Sample1.java:36)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1104)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1519)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1475)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)