需要导入相应的架包:
创建的上传页面: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>
点击下载:
点击保存,选择路径: