1、通过nginx服务器,上传文件
nginx 需要修改root 文件路径 ,在 nginx安装目录下,ngin.conf文件中
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root D:/Opt/nginx-1.17.2/images/;
index index.html index.htm;
}
FileUpLoadServlet 文件上传java代码
@MultipartConfig
@WebServlet(value = "/file", name = "FileServlet", loadOnStartup = 1)
public class FileUpLoadServletextends HttpServlet {
private String fileLocation = "D:/Opt/nginx-1.17.2/images/";
private String fileServer = "http://localhost/";
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
//获取上传的文件的所有的信息,part中封装要上传的文件的所有信息
// <input type="file" name="avatar"><br>
Part part = req.getPart("avatar");
// part.getName(); //通过part.getName() 得到不是文件名
//获取文件的名字, 文件名是在Part的头部, "content-disposition"中存储有文件名
// contentDisposition的数据内容是: form-data; name="avatar"; filename="4月8日复习.txt"
String contentDisposition = part.getHeader("content-disposition");
// System.out.println(contentDisposition);
String filePrefix = "filename=\""; // \n \t \"
//获取文件名开始位置所在的索引
int index = contentDisposition.indexOf(filePrefix) + filePrefix.length();
//获取文件名, contentDisposition.length() - 1 原因是因为最后有个 "
String fileName = contentDisposition.substring(index, contentDisposition.length() - 1);
// 获取文件的后缀名: .png .txt .jpg
String fileSuffix = fileName.substring(fileName.indexOf("."));
// UUID.randomUUID().toString() 随机生成一个字符串
String newFileName = UUID.randomUUID().toString() + fileSuffix;
InputStream is = part.getInputStream(); //获取文件的输入流
//文件输出流
OutputStream os = new FileOutputStream(fileLocation + newFileName);
// 存储IO流每次读取的数据
byte[] bs = new byte[1024];
int length = 0; //每次读取的长度
while(-1 != (length = is.read(bs))) {
os.write(bs, 0, length);
}
os.flush();
os.close();
is.close();
resp.setContentType("text/html;charset=utf-8");
PrintWriter writer = resp.getWriter();
StringBuffer html = new StringBuffer();
html.append("<html>")
.append("<head></head>")
.append("<body><h1>上传成功</h1>")
// fileServer + newFileName = http://localhost/0dc725b1-5b12-4865-bbd0-2fbed66b9e7e.png
.append("<img src=\"" + fileServer + newFileName + "\">")
.append("<video src=\"" + fileServer + newFileName + "\" width=\"600\" height=\"400\" autoplay>")
.append("</body></html>");
// String html = "<html><head></head><body><h1>注册成功</h1><p>" + user.getInterests() + "</p></body></html>";
writer.write(html.toString());
writer.flush();
writer.close();
}
}
fileupload.jsp
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="file" method="post" enctype="multipart/form-data" >
File: <input type="file" name="avatar"><br>
<input type="submit">
</form>
</body>
</html>
2、通过nginx服务器,下载文件 主要运用的是I/O流
获取指定文件夹下的所有文件,并实现下载
DownLoadServlet
@WebServlet(value = "/download",name = "DownloadServlet")
public class DownloadServlet extends HttpServlet {
//文件存放的地址
private String resourceLocation = "D:/Opt/nginx-1.17.2/images/";
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置字符格式
req.setCharacterEncoding("utf-8");
//获取的将要下载的文件名filename
String filename = req.getParameter("name");
InputStream in = new FileInputStream(resourceLocation+filename);//加载到输入流中
// 处理中文文件下载的时候,浏览器显示乱码的问题
String cnfilename = URLEncoder.encode(filename,"utf-8");
// 下载需要设置头信息, 是一种固定的模式
resp.setHeader("Content-Disposition", "attachment; filename="+cnfilename);
ServletOutputStream outputStream = resp.getOutputStream();//输出流
byte[] bs = new byte[3072];//字节数组
int length = 0;//数字默认长度为0
while ((length=in.read(bs))!=-1){//把输入流中的数据读到 bs里
outputStream.write(bs,0,length);//输出流写出
}
//释放资源
outputStream.flush();
outputStream.close();
in.close();
}
}
filedownload.jsp
<body>
<%
/*文件路径*/
String path = "D:/Opt/nginx-1.17.2/images/";
File file = new File(path);
/*判断是否是目录,然后遍历所有文件,并输出文件名*/
if (file.isDirectory()){
File[] fs = file.listFiles();
for (File f :fs){
String filename = f.getName();
%>
<%--下载文件--%>
<a href="download?name=<%=filename %>"><%=filename%></a><br>
<%
}
}
%>
</body>
</html>