jsp文件的上传与下载

需要导入相应的架包: 

创建的上传页面:uploadForm.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
    文件上传
    <hr>
    <form action="UploadServlet" method="post" enctype="multipart/form-data">
        请选择一个文件进行上传:
      <input type="file" name="myFile">
      <input type="submit" value="上传">
    </form>
    ${msg}
  </body>
</html>

Servlet:Upload.java:

package com.Servlet;

import java.io.IOException;

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

import com.jspsmart.upload.File;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
@WebServlet("/UploadServlet")
public class Upload extends HttpServlet {
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//SmartUpload负责文件的上传
		SmartUpload smartUpload=new SmartUpload();
		//初始化
		ServletConfig config=this.getServletConfig();
		smartUpload.initialize(config,request,response);
		try{
			//上传文件
			smartUpload.upload();
			//得到上传文件对象
			File smartFile=smartUpload.getFiles().getFile(0);
			//保存文件,保存到D盘,smartUpload.SAVE_PHYSICAL:表示按照硬盘上的物理路径保存
			smartFile.saveAs("D:/"+smartFile.getFileName(),smartUpload.SAVE_PHYSICAL);
			
		}catch(SmartUploadException e){
			e.printStackTrace();
		}
		//下面代码可以不回传到上个页面,可以新建一个jsp页面显示成功结果
		
		String msg="Upload Sucess!";
		request.setAttribute("msg", msg);
		RequestDispatcher rd=request.getRequestDispatcher("/uploadForm.jsp");
		rd.forward(request, response);
	}

}

结果:

选择文件

 

 点击上传:

查看D盘:

 

 文件下载页面:

uploadForm.jsp:

将连接目标执行下载的文件,例如:在本项目中/image下有一张图片:414.jpeg

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'download.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    文件下载:
    <hr>
    <a href="down.jsp?File=414.jpeg">下载</a>
  </body>
</html>

点击下载连接之后出现下载框,需要将连接目标定位到另外一个jsp:down.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'down.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <%
       String filename=request.getParameter("File");
       //告诉客户出现下载框,并指定下载框中的文件名
       response.setHeader("Content-Disposition","attachment;filename="+filename);
       //指定文件类型
       response.setContentType("image/jpeg");
       //指定文件
       RequestDispatcher rd=request.getRequestDispatcher("/image/"+filename);
       
       rd.forward(request, response);
       
     %>
  </body>
</html>

 

点击下载: 

 点击保存,选择路径:

 

  • 6
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

喵俺第一专栏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值