时间:2021-05-20
JavaWeb实现文件的上传与下载,供大家参考,具体内容如下
导入commons-fileupload-1.3.3.jar和commons-io-2.4.jar两个依赖包
1、提交页面 index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form action="${pageContext.request.contextPath }/fileUpload" method="post" enctype="multipart/form-data"> 头像:<input type="file" name="img" accept="image/*"/> <input type="submit" value="提交"/> </form> </body></html>2、结果页面 result.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html><html><head><meta charset="UTF-8"><title>Insert title here</title></head><body> <center> <a href="${pageContext.request.contextPath }/download?filename=${filename}" > <img alt="xx" src="${src }"> </a> </center></body></html>1、上传图片 fileUpload.java
package cn.yz123123.controller;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;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;@WebServlet("/fileUpload")@MultipartConfigpublic class fileUpload extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //String username = request.getParameter("username"); Part part = request.getPart("img"); //获取文件的真实名称 String header = part.getHeader("content-disposition"); String realName = header.substring(header.indexOf("filename=")+10, header.length()-1); //获取文件自身流 InputStream inputStream = part.getInputStream(); //获取file真实路径,如果没有则创建 String dir = request.getServletContext().getRealPath("/file/"); File dirFile = new File(dir); //上面只是实例化了一个对象,并没有真正的创建一个文件夹 if (!dirFile.exists()) { dirFile.mkdirs(); } //创建文件对象,并用流的形式写在相应的文件夹中 File file = new File(dir, realName); FileOutputStream fileOutputStream = new FileOutputStream(file); byte[] buf = new byte[1024]; int len; while((len=inputStream.read(buf))!=-1) { fileOutputStream.write(buf, 0, len); } fileOutputStream.close(); inputStream.close(); //以下为测试 request.setAttribute("src", request.getContextPath()+"/file/"+realName); request.setAttribute("filename", realName); request.getRequestDispatcher("/result.jsp").forward(request, response); } }2、下载图片 fileDownload.java
package cn.yz123123.controller;import java.io.FileInputStream;import java.io.IOException;import java.util.UUID;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;@WebServlet(urlPatterns = "/download")public class fileDownload extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String filename = req.getParameter("filename"); //获取文件的真实路径 String filePath = req.getServletContext().getRealPath("/file/"+filename); FileInputStream fileInputStream = new FileInputStream(filePath); resp.setCharacterEncoding("UTF-8"); resp.setHeader("Content-Disposition","attachment;filename="+UUID.randomUUID()+filename); ServletOutputStream outputStream = resp.getOutputStream(); byte[] buf = new byte[1024]; int len; while((len=fileInputStream.read(buf))!=-1) { outputStream.write(buf, 0, len); } outputStream.close(); fileInputStream.close(); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doGet(req, resp); }}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
在Web应用程序开发中,文件上传与下载功能是非常常用的功能,下面通过本文给大家介绍JavaWeb实现文件上传与下载实例详解。对于文件上传,浏览器在上传的过程中是
在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现,重点在文件上传 对于文件上传,浏览器在上传
本文实例为大家分享了javaweb文件的上传与下载,供大家参考,具体内容如下一、文件上传要实现Web开发的上传功能,通常需要完成两步操作:一是在Web页面中添加
本文实例为大家分享了javaweb文件下载的具体实现代码,供大家参考,具体内容如下文件上传示例注意:jsp页面编码为"UTF-8"文件上传的必要条件1.form
文件上传在Web应用中非常普遍,要在JavaWeb环境中实现文件上传功能非常容易,因为网上已经有许多用Java开发的组件用于文件上传,本文以使用最普遍的comm