如何使用JSP / Servlet将文件上传到服务器?

如何使用JSP / Servlet将文件上传到服务器?

如何使用JSP / Servlet将文件上传到服务器?我试过这个:


<form action="upload" method="post">

    <input type="text" name="description" />

    <input type="file" name="file" />

    <input type="submit" />

</form>

但是,我只获取文件名,而不是文件内容。当我添加  enctype="multipart/form-data"到<form>,然后request.getParameter()返回null。


在研究期间,我偶然发现了Apache Common FileUpload。我试过这个:


FileItemFactory factory = new DiskFileItemFactory();

ServletFileUpload upload = new ServletFileUpload(factory);

List items = upload.parseRequest(request); // This line is where it died.

不幸的是,servlet抛出了一个没有明确消息和原因的异常。这是堆栈跟踪:


SEVERE: Servlet.service() for servlet UploadServlet threw exception

javax.servlet.ServletException: Servlet execution threw an exception

    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:313)

    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)

    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)

    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)

    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)

    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)

    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)

    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)

    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)

    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)

    at java.lang.Thread.run(Thread.java:637)


繁星coding
浏览 1056回答 5
5回答

墨色风雨

我正在为每个 Html表单使用公共Servlet,无论它是否有附件。此Servlet返回一个TreeMap键,其中键是jsp名称参数和值是用户输入并将所有附件保存在固定目录中,稍后您重命名所选目录.Here Connections是具有连接对象的自定义接口。我想这会对你有所帮助public class ServletCommonfunctions extends HttpServlet implements&nbsp; &nbsp; &nbsp; &nbsp; Connections {&nbsp; &nbsp; private static final long serialVersionUID = 1L;&nbsp; &nbsp; public ServletCommonfunctions() {}&nbsp; &nbsp; protected void doPost(HttpServletRequest request,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HttpServletResponse response) throws ServletException,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IOException {}&nbsp; &nbsp; public SortedMap<String, String> savefilesindirectory(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HttpServletRequest request, HttpServletResponse response)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; // Map<String, String> key_values = Collections.synchronizedMap( new&nbsp; &nbsp; &nbsp; &nbsp; // TreeMap<String, String>());&nbsp; &nbsp; &nbsp; &nbsp; SortedMap<String, String> key_values = new TreeMap<String, String>();&nbsp; &nbsp; &nbsp; &nbsp; String dist = null, fact = null;&nbsp; &nbsp; &nbsp; &nbsp; PrintWriter out = response.getWriter();&nbsp; &nbsp; &nbsp; &nbsp; File file;&nbsp; &nbsp; &nbsp; &nbsp; String filePath = "E:\\FSPATH1\\2KL06CS048\\";&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Directory Created&nbsp; &nbsp;????????????"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + new File(filePath).mkdir());&nbsp; &nbsp; &nbsp; &nbsp; int maxFileSize = 5000 * 1024;&nbsp; &nbsp; &nbsp; &nbsp; int maxMemSize = 5000 * 1024;&nbsp; &nbsp; &nbsp; &nbsp; // Verify the content type&nbsp; &nbsp; &nbsp; &nbsp; String contentType = request.getContentType();&nbsp; &nbsp; &nbsp; &nbsp; if ((contentType.indexOf("multipart/form-data") >= 0)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DiskFileItemFactory factory = new DiskFileItemFactory();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // maximum size that will be stored in memory&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; factory.setSizeThreshold(maxMemSize);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Location to save data that is larger than maxMemSize.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; factory.setRepository(new File(filePath));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Create a new file upload handler&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ServletFileUpload upload = new ServletFileUpload(factory);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // maximum file size to be uploaded.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; upload.setSizeMax(maxFileSize);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Parse the request to get file items.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @SuppressWarnings("unchecked")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<FileItem> fileItems = upload.parseRequest(request);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Process the uploaded file items&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Iterator<FileItem> i = fileItems.iterator();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (i.hasNext()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FileItem fi = (FileItem) i.next();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!fi.isFormField()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Get the uploaded file parameters&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String fileName = fi.getName();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Write the file&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (fileName.lastIndexOf("\\") >= 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; file = new File(filePath&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + fileName.substring(fileName&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .lastIndexOf("\\")));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; file = new File(filePath&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + fileName.substring(fileName&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .lastIndexOf("\\") + 1));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fi.write(file);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; key_values.put(fi.getFieldName(), fi.getString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception ex) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(ex);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return key_values;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java