上传下载文件
1.文件上传---JSP+Servlet
准备工作:
- jspsamrt.jar包下载
链接:https://pan.baidu.com/s/1p56ZLlGvAmnDfVegjJ86TQ 提取码:u8ig
- 然后将该包复制到WEB-INF的lib路径下。
uploadForm.jsp 下面的页面字符集为gb2312,为了解决乱码问题
<%@ page language="java" contentType="text/html; charset=gb2312"
pageEncoding="utf-8"%>
<html>
<body>
<center>文件上传</center>
<hr>
<form action="UploadServlet" method="POST" enctype="multipart/form-data">
请选择一个文件上传
<input type="file" name="myFile">
<input type="submit" name="上传">
</form>
<br>${msg}
</body>
</html>
上传文件的Servlet--------UploadServlet.java
package edu.ts.pro.servlets;
import com.jspsmart.upload.File;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
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 java.io.IOException;
@WebServlet(name = "UploadServlet",urlPatterns = "/UploadServlet")
public class UploadServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
SmartUpload smartUpload=new SmartUpload();
//初始化
ServletConfig config=this.getServletConfig();
smartUpload.initialize(config, request, response);
try {
//上传文件
smartUpload.upload();
//收到上传的文件对象
File smartFile=smartUpload.getFiles().getFile(0);
//保存文件
System.out.println(smartFile.getFileName());
String str=new String(smartFile.getFileName().getBytes("iso-8859-1"),"GBK");
smartFile.saveAs("d:/"+str,smartUpload.SAVE_PHYSICAL);
}catch(SmartUploadException e) {
e.printStackTrace();
}
String msg="upload success!";
request.setAttribute("msg", msg);
RequestDispatcher rd=request.getRequestDispatcher("formup.jsp");
rd.forward(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
2.文件下载jsp+servlet
准备工作:
- 在d盘保存aa.txt文件
download.jsp
<%@ page language="java" contentType="text/html; charset=gb2312"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<body>
文件下载
<br>
<hr>
<br>
<a href="downLoad?file=aa.txt">download</a>
</body>
</html>
DownLoad.java
package edu.ts.pro.servlets;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
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 javax.servlet.jsp.PageContext;
import java.io.IOException;
@WebServlet(name = "DownloadServlet",urlPatterns = "/downLoad")
public class DownloadServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String filename=request.getParameter("file");
SmartUpload smartUpload = new SmartUpload();
//初始化
ServletConfig config=this.getServletConfig();
smartUpload.initialize(config, request, response);
// 设定contentDisposition为null以禁止浏览器自动打开文件,
//保证点击链接后是下载文件。若不设定,则下载的文件扩展名为doc时,浏览器将自动用word打开它。
// 扩展名为pdf时,浏览器将用acrobat打开。
smartUpload.setContentDisposition(null);
// 下载文件
try {
smartUpload.downloadFile("d:/"+filename);
} catch (SmartUploadException e) {
e.printStackTrace();
}
}
}