JavaWeb实现文件的上传与下载
程序员文章站
2022-07-03 19:10:42
javaweb实现文件的上传与下载,供大家参考,具体内容如下第一步:导包导入commons-fileupload-1.3.3.jar和commons-io-2.4.jar两个依赖包第二步:编写前端页面...
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") @multipartconfig public 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); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。