
文件能够正确的上传回显,但是下载的时候后台总是会报文件找不到的错误,(http:\localhost:80\gms\upload\123456.xlsx (文件名、目录名或卷标语法不正确。))但是这个路径直接贴到浏览器里面是可以下载到的,下载的代码如下:
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { request.setCharacterEncoding("UTF-8"); String downFilename = request.getParameter("curfile"); String filepath = request.getParameter("path"); response.setContentType("text/plain"); response.setHeader("Location", downFilename); response.setHeader("Content-Disposition", "attachment; filename=" + downFilename); OutputStream outputStream = response.getOutputStream(); InputStream inputStream = new FileInputStream(filepath); byte[] buffer = new byte[1024]; int i = -1; while ((i = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, i); } outputStream.flush(); outputStream.close(); } catch (FileNotFoundException e1) { e1.printStackTrace(); System.out.println("没有找到您要的文件"); } catch (Exception e) { e.printStackTrace(); System.out.println("系统错误,请及时与管理员联系"); } } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } 1 odirus 2016 年 8 月 18 日 https://commons.apache.org/proper/commons-io/javadocs/api-2.5/index.html copyURLToFile 用了这个函数,整个世界都安静了。 |
2 hcymk2 2016 年 8 月 18 日 |
3 odirus 2016 年 8 月 18 日 另外你这个地址 http:\localhost:80\gms\upload\123456.xlsx 换成 http://localhost:80\gms\upload\123456.xlsx 试试 还不行的话再试试 http://127.0.0.1:80\gms\upload\123456.xlsx 现在的浏览器都很智能,一些常见的 URL 错误能够给你自动纠正,但你的程序科没那么聪明哦 |
4 Izual_Yang 2016 年 8 月 22 日 兰州不会一直这么写 url 吧 |