文件无法上传到服务器

问题:1.无法上传文件到服务器
2.单独运行DownServlet可以看到相关文件
3上传文件后无法查看和下载
我想应该是DownloadServlet.java出现问题,看了一天也没发现,求帮助
上传后点下载会这样

img

img

1.uploadHttpOne.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
html>
<html lang="zh-cn">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="renderer" content="webkit">
<title>单个文件上传title>
<link rel="stylesheet" href="css/pintuer.css">
<link rel="stylesheet" href="css/admin.css">
<script src="js/jquery.js">script>
<script src="js/pintuer.js">script>
head>
<body>
<div class="panel admin-panel">
<div class="panel-head"><strong><span class="icon-pencil-square-o">span>单文件上传实例strong>
<a href="index.jsp" style="float:right;">-->返回首页a>div>
<div class="body-content">
<form action="uploadHttpOneServlet" method="post" class="form-x" enctype="multipart/form-data">
<div class="form-group">
<div class="label">
<label>文件描述:label>
div>
<div class="field">
<input type="text" class="input" name="filediscription" value=""/>
<div class="tips">div>
div>
div>
<div class="form-group">
<div class="label">
<label>请选择文件label>
div>
<div class="field">
<input type="file" id="url1" name="resPath" class="input input-file tips"
style="width:25%;float:left;" value="+浏览上传" data-toggle="hover"
data-place="right" data-image="" accept=".jpg,.png,.gif"/>
div>
div>
<div class="form-group">
<div class="label">
<label>label>
div>
<div class="field">
<button class="button bg-main icon-check-square-o" type="submit">提交button>
<button class="button bg-main icon-check-square-o" type="reset">重置button>
div>
div>
form>
div>
div>
body>html>


2.UploadHttpOneServlet.java

package dl_servlet;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import util.MyUtil;

/**
 * Servlet implementation class UploadHttpOneServlet
 */
@WebServlet(description="上传文件的servlet类",urlPatterns= {"/uploadHttpOneServlet"})
@MultipartConfig(maxFileSize=20*1024*1024)
public class UploadHttpOneServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UploadHttpOneServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    
        doPost(request,response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out=response.getWriter();
        HttpServletRequest req=(HttpServletRequest) request;
        String path=req.getContextPath();
        Date date=new Date();
        SimpleDateFormat dateFormat=new SimpleDateFormat("YYYYMMddHHmmss");
        String uploadfileDate=dateFormat.format(date);
        request.setCharacterEncoding("utf-8");
        Part part=request.getPart("resPath");
        String filediscription=request.getParameter("filediscription");
        out.println("输入的文件描述为:"+filediscription+"
"
); File uploadFileDir = new File(getServletContext().getRealPath("uploadFiles")); if (!uploadFileDir.exists()) { uploadFileDir.mkdir(); } //获取原始名字 String oldName=MyUtil.getFileName(part); out.println("上传文件的原始名称为"+oldName+"
"
); out.println("上传文件的大小为"+part.getSize()/1024+"KB
"
); out.println("文件上传到:"+ uploadFileDir + File.separator + oldName + "
"
); out.println("如需查看请点击:"+""uploadFiles"+File.separator+uploadfileDate+ "-"+oldName+">下载文件"); if(oldName!=null) {part.write(uploadFileDir + File.separator + oldName);} else { out.println(""); } } }

3.DownloadServlet.java

package dl_servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class DownloadServlet
 */
@WebServlet(name = "DownloadServlet",urlPatterns = "/downloadServlet")
public class DownloadServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DownloadServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doPost(request,response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        
        File downLoadFileDir=new File(getServletContext().getRealPath("/uploadFiles"));
        String aFileName=null;
        FileInputStream in=null;//输入流
        ServletOutputStream out =null;
        request.setCharacterEncoding("utf-8");
        try {
            aFileName=request.getParameter("resPath");
            response.setHeader("Content-Type", "application/x-msdownload");
            response.setHeader("Content-Disposition", "attachment;filename="+util.MyUtil.toUTF8String(aFileName));
            in=new FileInputStream(downLoadFileDir+File.separator+aFileName);
            out=response.getOutputStream();
            out.flush();
            int aRead=0;
            byte b[]=new byte[1024];
            while((aRead=in.read(b))!=-1&in !=null) {
                out.write(b, 0, aRead);
            }
            out.flush();
            in.close();
            out.close();
        }
        catch(Throwable e) {
            e.printStackTrace();
        }
    }

}
4.ShowDownServlet.java

```java
package dl_servlet;

import java.io.File;
import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ShowDownServlet
 */
@WebServlet(name ="ShowDownServlet" ,urlPatterns ="/showDownServlet")
public class ShowDownServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ShowDownServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        File downLoadFileDir=new File(getServletContext().getRealPath("/uploadFiles"));
         File[] list = downLoadFileDir.listFiles();
         request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        request.setAttribute("filelist",list);
        RequestDispatcher dis=request.getRequestDispatcher("showFileList.jsp");
        dis.forward(request, response);
        
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request,response);
        
        
    }

}


5.showFileList.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"  %>
html>
<html lang="zh-cn">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="renderer" content="webkit">
<title>上传文件下载页面title>
<link rel="stylesheet" href="css/pintuer.css">
<link rel="stylesheet" href="css/admin.css">
<script src="js/jquery.js">script>
<script src="js/pintuer.js">script>
head>
<body>
 <div class="panel admin-panel">
 <div class="panel-head"><strong class="icon-reorder">上传文件信息展示和下载列表strong>
 <a href="index.jsp" style="float:right;">-->返回首页a>div>
 <table class="table table-hover text-center">
 <tr>
 <th width="100" style="text-align:left;padding-left:20px;">IDth>
 <th width="10%">序号th>
 <th>名称th>
 <th width="10%">上传时间th>
 <th width=310>操作th>
 tr>
 <c:forEach var="afile" items="${filelist}" varStatus="i">
 <tr>
 <td style="text-align:left;padding-left:20px;"><input type="checkbox" name="id[]" value=""/>
 ${i.count }td>
 <td><input type="text" name="sort[1]" value="${i.count}" style="width:50px;text-align:center;
 border:1px solid #ddd; padding:7px 0;"/>td>
 <td>${afile.name}td>
 <td>2019-10-15td>
 <td><div class="button-group"> <a class="button border-main"
 href="downloadServlet?resPath=${afile.name}">
 <span class="icon-edit">span>下载文件a>div>td>
 <tr>
 c:forEach>
 <tr>
 <td colspan="8"><div class="pagelist"><a href="#">上一页a>
 <span class="current">1span><a href="#">2a>
 <a href="#">下一页a><a href="#">尾页a>div>td>
 tr>
 
 table>
 div>
<script type="text/javascript">

//单个删除
function del(id,mid,iscid){
    if(confirm("您确定要删除吗?")){
        
    }
}

//全选
$("#checkall").click(function(){ 
  $("input[name='id[]']").each(function(){
      if (this.checked) {
          this.checked = false;
      }
      else {
          this.checked = true;
      }
  });
})



//批量排序
function sorts(){
    var Checkbox=false;
     $("input[name='id[]']").each(function(){
      if (this.checked==true) {        
        Checkbox=true;    
      }
    });
    if (Checkbox){    
        
        $("#listform").submit();        
    }
    else{
        alert("请选择要操作的内容!");
        return false;
    }
}




script>
body>
html>


图片的访问地址写错了,fileupload-stu后面有一个/,http://localhost:8080/fileupload-stu/uploadFiles/20221108001817-y.jpg