上传页面jsp文件或者html都可以
form method属性设置为post enctype设置为multipart/form-data
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form method="post" action="filer" enctype="multipart/form-data">
选择一个文件:
<input type="file" name="uploadFile" />
<br/><br/>
<input type="submit" value="上传" />
</form>
</body>
</html>
发给对应的serverlt处理
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;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
@WebServlet("/filer")
@MultipartConfig
public class filer extends HttpServlet {
final static String ML="你的路径";
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取传入的文件
Part filePart = req.getPart("uploadFile");
//创建流
InputStream inputStream = filePart.getInputStream();
FileOutputStream fileOut = new FileOutputStream(ML+filePart.getSubmittedFileName());
byte[] buffer = new byte[4096];
int bytesRead = -1;
// 读取输入流并写入文件输出流
while ((bytesRead = inputStream.read(buffer)) != -1) {
fileOut.write(buffer, 0, bytesRead);
}
// 关闭流
fileOut.close();
inputStream.close();
req.getRequestDispatcher("sccj.jsp").forward(req, resp);
}
}
下面是下载部分 serverlt代码,其中doget用于返回路径下所有文件,不包括文件夹
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;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
@WebServlet("/dow")
public class filerdow extends HttpServlet {
final static String filePath = "C:\\Users\\86153\\Desktop\\笔记\\";
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取要下载的文件的绝对路径和文件名
String fileName = request.getParameter("name");
// 设置响应头,控制浏览器以下载的形式打开文件
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
// 获取要下载的文件的输入流
FileInputStream fileInputStream = new FileInputStream(filePath + fileName);
// 将文件输入流设置到HTTP响应的输出流中
ServletOutputStream outputStream = response.getOutputStream();
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = fileInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
fileInputStream.close();
outputStream.flush();
outputStream.close();
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
File file = new File(filePath);
File[] files = file.listFiles();
List<String> list = new ArrayList<>();
for (File file1 : files) {
if (file1.isFile()){
list.add(file1.getName());
}
}
req.setAttribute("list", list);
req.getRequestDispatcher("dow.jsp").forward(req, resp);
}
}
这是前端的下载代码,我用的是from表单input的submit 点击哪个文件返给serverlt并下载
<%@ page import="java.util.List" %>
<%@ page import="java.io.File" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page pageEncoding="UTF-8" %>
<!DOCTYPE html>
<%
List<String> s= (List<String>) request.getAttribute("list");
%>
<html>
<form action="dow" method="post">
<% for(int i=0;i<s.size();i++){%>
<br><br>
<input type="submit" name="name" value="<%=s.get(i)%>">
<% } %>
</form>
<a href="index.jsp">返回</a>
</html>