环境搭建
目的:在工程下创建文件夹并接收上传的文件UploadServletpublic class UploadServlet extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {try {// 1.创建磁盘文件项工厂DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();// 2.创建一个核心的解析类ServletFileUpload fileUpload = new ServletFileUpload(diskFileItemFactory);// 3.利用核心类解析Request,解析后会得到多个部分。返回一个List集合。List集合装的是每个部分的内容(FileItem文件项)List<FileItem> list = fileUpload.parseRequest(request);// 4.遍历List集合,会得到代表每个部分的文件项的对象。根据文件项判断是否是文件上传项for (FileItem fileItem : list) {// 判断这个文件项是否是普通项还是文件上传项。if (fileItem.isFormField()) {// 普通项:// 接收普通项的值:(接收值不能再使用request.getParameter())String name = fileItem.getFieldName();// 获得普通项的名称// 获得普通项的值String value = fileItem.getString("UTF-8");System.out.println(name + " " + value);} else {// 文件上传项:// 获得文件上传项的文件的名称:String fileName = fileItem.getName();// 获得文件上传的文件的数据:InputStream is = fileItem.getInputStream();// 获得文件上传的路径:磁盘绝对路径。String realPath = getServletContext().getRealPath("/upload");// 创建一个输出流,写入到设置的路径中。OutputStream os = new FileOutputStream(realPath + "/" + fileName);// 两个流对接:int len = 0;byte[] b = new byte[1024];while ((len = is.read(b)) != -1) {os.write(b, 0, len);}is.close();os.close();}}} catch (Exception e) {e.printStackTrace();}}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}}
upload.jsp
<h1>文件上传</h1><!-- 文件上传三要素* 表单需要是post提交* 表单中需要文件上传项,必须有name的属性和值* 表单的enctype属性必须是multipart/form-data --><form action="${ pageContext.request.contextPath}/UploadServlet" method="post" enctype="multipart/form-data">文件描述:<input type="text" name="info"><br>文件上传:<input type="file" name="upload"><br><input type="submit" value="上传"></form>
代码报错
java.io.FileNotFoundException: E:\IdeaProjects\web_test\out\artifacts\web06_war_exploded\upload\aa.txt (系统找不到指定的路径。)at java.io.FileOutputStream.open0(Native Method)at java.io.FileOutputStream.open(FileOutputStream.java:270)at java.io.FileOutputStream.<init>(FileOutputStream.java:213)at java.io.FileOutputStream.<init>(FileOutputStream.java:101)at upload.UploadServlet.doPost(UploadServlet.java:48)at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668)at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:770)at org.apache..NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415)at org.apache..SocketProcessorBase.run(SocketProcessorBase.java:49)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)at java.lang.Thread.run(Thread.java:748)
问题
不会在项目文件夹里生成upload文件夹而且生成的位置在out目录下
解决方法1
自己创建路径去接收
OutputStream os = new FileOutputStream("E:\\" + fileName);
解决方法2
自己添加一个创建文件夹的方法
String path="E:\\mydata\\upload\\";File file = new File(path+fileName);if (!file.getParentFile().exists()){file.getParentFile().mkdirs();}// 创建一个输出流,写入到设置的路径中。OutputStream os = new FileOutputStream(file);