这是我的Upload.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="UploadServlet" enctype="multipart/form-data" method="post">
Select File : <input type="file" name="video"><br>
<input type="submit" name="upload" value="Upload">
</form>
</body>
</html>
这是我的UploadServlet.java
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
boolean isMultiPart = ServletFileUpload.isMultipartContent(request);
List<FileItem> items = null;
if(isMultiPart)
{
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try {
items = upload.parseRequest(request);
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Iterator<FileItem> it = items.iterator();
while(it.hasNext())
{
FileItem item = (FileItem) it.next();
if(!(item.isFormField()))
{
String fieldname = item.getFieldName();
String filename = item.getName();
long fileSize = item.getSize();
System.out.println("Field Name is :"+fieldname);
System.out.println("File Name is :"+filename);
System.out.println("File Size is :"+fileSize);
String filepath = "C:"+File.separator;
File video = new File(filepath);
FileOutputStream foutput = new FileOutputStream(video);
DeferredFileOutputStream fout = new DeferredFileOutputStream(1, video);
fout.writeTo(foutput);
try {
item.write(video);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
当我尝试上传文件时,它正在抛出java.lang.ClassNotFoundException:org.apache.commons.io.output.DeferredFileOutputStream&#39; at&#39; upload.parseRequest(request)&#39;方法。我也可以使用相同的代码上传视频。如果不喜欢上传视频应该采用的过程。
下面是我的Stack Trace:
java.lang.ClassNotFoundException: org.apache.commons.io.output.DeferredFileOutputStream
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
at org.apache.commons.fileupload.disk.DiskFileItemFactory.createItem(DiskFileItemFactory.java:199)
at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:361)
at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:126)
at com.videoplayer.controller.UploadServlet.doPost(UploadServlet.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
答案 0 :(得分:0)
您可以使用下一个代码
try {
Part filePart = request.getPart("poster");//name of your parameter
String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString();
String webInfPath = request.getServletContext().getRealPath("images");
File file = new File(webInfPath,fileName);
InputStream fileContent = filePart.getInputStream();
FileOutputStream fileOutputStream = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int len = fileContent.read(buffer);
while (len != -1) {
fileOutputStream.write(buffer, 0, len);
len = fileContent.read(buffer);
}
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
System.out.println("can't close file");
}
}
} catch (ServletException | IOException e) {
throw new ServiceException("error of upload");
}