JavaWeb實(shí)現(xiàn)文件的上傳與下載
JavaWeb實(shí)現(xiàn)文件的上傳與下載,供大家參考,具體內(nèi)容如下
第一步:導(dǎo)包
導(dǎo)入commons-fileupload-1.3.3.jar和commons-io-2.4.jar兩個(gè)依賴包
第二步:編寫前端頁面
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、結(jié)果頁面 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");
//獲取文件的真實(shí)名稱
String header = part.getHeader("content-disposition");
String realName = header.substring(header.indexOf("filename=")+10, header.length()-1);
//獲取文件自身流
InputStream inputStream = part.getInputStream();
//獲取file真實(shí)路徑,如果沒有則創(chuàng)建
String dir = request.getServletContext().getRealPath("/file/");
File dirFile = new File(dir);
//上面只是實(shí)例化了一個(gè)對(duì)象,并沒有真正的創(chuàng)建一個(gè)文件夾
if (!dirFile.exists()) {
dirFile.mkdirs();
}
//創(chuàng)建文件對(duì)象,并用流的形式寫在相應(yīng)的文件夾中
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");
//獲取文件的真實(shí)路徑
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);
}
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot actuator生產(chǎn)就緒功能實(shí)現(xiàn)解析
這篇文章主要介紹了Springboot actuator生產(chǎn)就緒功能實(shí)現(xiàn)解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
關(guān)于Mybatis中foreach遍歷Map的實(shí)現(xiàn)示例
這篇文章主要介紹了關(guān)于Mybatis中foreach遍歷Map的實(shí)現(xiàn)示例,MyBatis?是一款優(yōu)秀的半自動(dòng)的ORM持久層框架,它支持自定義?SQL、存儲(chǔ)過程以及高級(jí)映射,需要的朋友可以參考下2023-05-05
Java?SpringBoot?@Async實(shí)現(xiàn)異步任務(wù)的流程分析
這篇文章主要介紹了Java?SpringBoot?@Async實(shí)現(xiàn)異步任務(wù),主要包括@Async?異步任務(wù)-無返回值,@Async?異步任務(wù)-有返回值,@Async?+?自定義線程池的操作代碼,需要的朋友可以參考下2022-12-12
java數(shù)據(jù)庫開發(fā)之JDBC基礎(chǔ)使用方法及實(shí)例詳解
這篇文章主要介紹了java數(shù)據(jù)庫開發(fā)之JDBC基礎(chǔ)知識(shí)詳解,需要的朋友可以參考下2020-02-02
maven項(xiàng)目下solr和spring的整合配置詳解
這篇文章主要介紹了maven項(xiàng)目下solr和spring的整合配置詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-11-11
java實(shí)現(xiàn)基于Tcp的socket聊天程序
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)基于Tcp的socket聊天程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
Java設(shè)計(jì)模式常用的七大原則總結(jié)
今天給大家總結(jié)了Java設(shè)計(jì)模式的七大原則,主要有單一職責(zé)原則,接口隔離原則,依賴倒轉(zhuǎn)原則,里氏替換原則等,文中有非常詳細(xì)的介紹,需要的朋友可以參考下2021-06-06
Java?pdf文件書簽承前縮放驗(yàn)證的設(shè)置方法
很多朋友不知道是什么是書簽承前縮放,簡單說就是可以任意改變當(dāng)前pdf文檔縮放比例,點(diǎn)擊書簽后不影響其縮放比率,本文給大家介紹下Java?pdf文件書簽承前縮放驗(yàn)證的設(shè)置方法,感興趣的朋友一起看看吧2022-02-02
Java與Mysql鎖相關(guān)知識(shí)總結(jié)
這篇文章主要介紹了Java與Mysql鎖相關(guān)知識(shí)總結(jié)的相關(guān)資料,需要的朋友可以參考下2023-04-04

