1、JSP页面代码实现

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>UploadDown</title>
</head>
<body>
<%
if(null==session.getAttribute("currentUser")){
System.out.print("ddd");
response.sendRedirect("/login");
return;
}else{
System.out.print("yyy");
}
%>
<div align="center">
<form action="${pageContext.request.contextPath }/file/upload"
method="post" enctype="multipart/form-data">
<input type="file" name="file" width="120px">
<input type="submit" value="上传">
</form>
<br>
</div> <div>
<table border="1px" bordercolor="yellow" align="center">
<tr>
<th colspan="5" style="font-size: 25px">目录下可下载文件</th>
</tr>
<tr>
<th width="66px">序号</th>
<th width="150px">文件</th>
<th width="150px">大小</th>
<th width="200px">上传时间</th>
<th width="150px">下载</th>
</tr>
<c:forEach items="${fileList }" var="file" varStatus="s"> <jsp:useBean id="dateValue" class="java.util.Date"/>
<jsp:setProperty name="dateValue" property="time" value="${file.lastModified()}"/> <tr>
<td align="center">${s.count}</td>
<td align="center">${file.getName() }</td>
<td align="center">
<fmt:formatNumber type="number" value="${file.length()/1024.00}" pattern="#0.00"/> KB
</td>
<td align="center">
<fmt:formatDate value="${dateValue}" pattern="yyyy-MM-dd HH:mm:ss"/>
</td>
<td align="center">
<input type="button" value="下载" onclick="window.location.href='${pageContext.request.contextPath }/file/down?filename=${file.getName()}'">
<input type="button" value="删除" onclick="window.location.href='${pageContext.request.contextPath }/file/deleteFile?filename=${file.getName()}'"> <%-- <button>
<a href="${pageContext.request.contextPath }/file/deleteFile?filename=${file.getName()}" style="text-decoration: none">删除</a>
</button> --%>
</td>
</tr>
</c:forEach>
</table>
</div>
</body>
<canvas id="canvas" style="position: fixed;"></canvas>
<script src="./js/js1.js"></script>
<style type="text/css">
body{
background-image: url(./js/bg1.jpeg);
background-size:cover;
}
</style>
</html>

2、上传下载代码实现

package com.chao.controller;

import com.chao.utils.PathUtil;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest; @Controller
@RequestMapping({ "file" })
public class UploadDownController {
@RequestMapping
public String toIndex(Model model) throws Exception {
String path = PathUtil.static_root;
File file = new File(path);
List fileList = new ArrayList(); if (file != null) {
if (file.isDirectory()) {
File[] fileArray = file.listFiles();
if ((fileArray != null) && (fileArray.length > 0))
for (File f : fileArray)
fileList.add(f);
} else {
System.out.println("目录不存在!");
}
}
model.addAttribute("fileList", fileList);
model.addAttribute("path", path);
return "index";
} @RequestMapping(value = { "upload" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
@ResponseBody
public void upload(HttpServletRequest request, HttpServletResponse response) throws Exception {
MultipartRequest multipartRequest = (MultipartRequest) request;
MultipartFile file = multipartRequest.getFile("file"); if ((file == null) || (file.isEmpty())) {
response.setCharacterEncoding("GB2312");
PrintWriter out = response.getWriter();
out.print("<script>alert('请选择上传文件!'); window.location='/file' </script>");
out.flush();
out.close();
} else {
String fileName = file.getOriginalFilename();
String path = PathUtil.static_root; File dir = new File(path, fileName);
if (!dir.getParentFile().exists()) {
dir.getParentFile().mkdirs(); dir.createNewFile();
} else {
dir.createNewFile();
} file.transferTo(dir);
response.sendRedirect("/file");
}
} @RequestMapping({ "down" })
public void down(String filename, HttpServletRequest request, HttpServletResponse response) throws Exception {
String path = PathUtil.static_root;
String downFileName = new String(filename.getBytes("ISO8859-1"), "utf-8"); String fileName = path + File.separator + downFileName; InputStream bis = new BufferedInputStream(new FileInputStream(new File(fileName)));
response.addHeader("Content-Disposition", "attachment;filename=" + filename);
response.setContentType("multipart/form-data"); BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
int len = 0;
while ((len = bis.read()) != -1) {
out.write(len);
out.flush();
}
out.close();
} @RequestMapping({ "deleteFile" })
public String deleteFile(String filename) throws Exception {
String path = PathUtil.static_root;
filename = new String(filename.getBytes("ISO8859-1"), "utf-8");
String fileName = path + File.separator + filename; File file = new File(fileName);
if ((file.exists()) && (file.isFile()))
file.delete();
else {
return "error";
}
return "redirect:/file";
}
}

3、文件上传环境路径判断

package com.chao.utils;

public class PathUtil {

    public static final String WINDOWS_STATIC = "D:\\uploadfile";// Windows静态文件路径

    public static final String LINUX_STATIC = "/uploadfile";// Linux静态文件路径

    public static String static_root = null;

    static {
String system = System.getProperties().getProperty("os.name"); // 获取系统类型
if (system.contains("Windows"))
static_root = WINDOWS_STATIC;
if (system.contains("Linux"))
static_root = LINUX_STATIC;
}
}

4、mysql数据库

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0; -- ----------------------------
-- Table structure for users
-- ----------------------------
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`id` int(0) NOT NULL AUTO_INCREMENT,
`username` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`password` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; SET FOREIGN_KEY_CHECKS = 1;

Java-Maven实现简单的文件上传下载(菜鸟一枚、仅供参考)的更多相关文章

  1. Java实现一个简单的文件上传案例

    Java实现一个简单的文件上传案例 实现流程: 1.客户端从硬盘读取文件数据到程序中 2.客户端输出流,写出文件到服务端 3.服务端输出流,读取文件数据到服务端中 4.输出流,写出文件数据到服务器硬盘 ...

  2. Java 客户端操作 FastDFS 实现文件上传下载替换删除

    FastDFS 的作者余庆先生已经为我们开发好了 Java 对应的 SDK.这里需要解释一下:作者余庆并没有及时更新最新的 Java SDK 至 Maven 中央仓库,目前中央仓库最新版仍旧是 1.2 ...

  3. Java实现FTP批量大文件上传下载篇1

    本文介绍了在Java中,如何使用Java现有的可用的库来编写FTP客户端代码,并开发成Applet控件,做成基于Web的批量.大文件的上传下载控件.文章在比较了一系列FTP客户库的基础上,就其中一个比 ...

  4. JAVA中使用FTPClient实现文件上传下载

    在JAVA程序中,经常需要和FTP打交道,比如向FTP服务器上传文件.下载文件,本文简单介绍如何利用jakarta commons中的FTPClient(在commons-net包中)实现上传下载文件 ...

  5. Java语言实现简单FTP软件------>上传下载管理模块的实现(十一)

    1.上传本地文件或文件夹到远程FTP服务器端的功能. 当用户在本地文件列表中选择想要上传的文件后,点击上传按钮,将本机上指定的文件上传到FTP服务器当前展现的目录,下图为上传子模块流程图 选择好要上传 ...

  6. JAVA中使用FTPClient实现文件上传下载实例代码

    一.上传文件 原理就不介绍了,大家直接看代码吧 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 ...

  7. Java语言实现简单FTP软件------>上传下载队列窗口的实现(七)

    1.首先看一下队列窗口的界面 2.看一下上传队列窗口的界面 3.看一下下载队列窗口的界面 4.队列窗口的实现 package com.oyp.ftp.panel.queue; import stati ...

  8. Java实现FTP与SFTP文件上传下载

    添加依赖Jsch-0.1.54.jar <!-- https://mvnrepository.com/artifact/com.jcraft/jsch --> <dependency ...

  9. java中io流实现文件上传下载

    新建io.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" page ...

  10. java操作FTP,实现文件上传下载删除操作

    上传文件到FTP服务器: /** * Description: 向FTP服务器上传文件 * @param url FTP服务器hostname * @param port FTP服务器端口,如果默认端 ...

随机推荐

  1. Dockerfile打包java应用

    #基础镜像 FROM openjdk:8-jre # 作者 MAINTAINER hg #挂载目录 VOLUME /home/hg #创建目录 RUN mkdir /home/hg #指定工作目录 W ...

  2. Curl 命令举例

    curl是一个强大的网络请求lib,虽然强大,但是参数太多哈,不得不吐槽,下面列举一下常用使用方法,供大家CV POST 请求 入门: curl -X POST "http://localh ...

  3. 电脑安装不同版本的QT

    系统:windows Qt版本:5.10/5.13 电脑中已经安装5.10版本的,安装5.13版本的不知道是否会出现问题,故此记录. 下载安装Qt5.13,安装过程跟之前版本的基本没差别,安装路径 D ...

  4. 5. nginx跨域配置

    1.跨域问题处理:在nginx相关接口上配置如下: 如接口有自己的请求头,则加上:如接口自带请求头pubacc-buid if ($request_method = "OPTIONS&quo ...

  5. 【杂项】利用CUDA实现tensorflow的gpu加速——以NXP的eIQ Portal Command line环境为例

    这是一个针对于eIQ的解决方案,笔者所用显卡是GTX1650 step1:下载CUDA和CuDnn 2022年3月,eIQ所使用tensorflow版本为2.5.0,因此对应CUDA 11.2.0,C ...

  6. React-Navigation 5.x 的 demo案例

    一. stack路由结构的一些效果 (1)横向过渡动画 (2)整个选项卡样式修饰 最终实现效果:动态图 以上两个功能实现都很简单,我测试时,关注了一个问题,navigation 丢失.stackNav ...

  7. docker+react+nginx部署

    一.准备工作 1.先确保项目可以正常运行. 2.如果拉代码到Linux下进行打包,注意node版本,我就是版本不同,yarn build一直不成功. 3.找一个nginx的配置文件nginx.conf ...

  8. [转]md Typora旧版免费安装包(多平台版本)

    typora旧版免费安装包: 链接:https://pan.baidu.com/s/1pIqeO2nTJ9_s16IZj6z3sA?pwd=gut4 提取码:gut4 v1.0以上的版本都要收费了,我 ...

  9. linux安装Elasticsearch的单节点

    一.基础环境 操作系统环境:Red Hat Enterprise Linux Server release 6.4 (Santiago) ES版本:elasticsearch-7.8.0-linux- ...

  10. MyBatis_07(动态SQL)

    动态SQL: Mybatis框架的动态SQL技术是一种根据特定条件动态拼装SQL语句的功能, 它存在的意义是:"为了解决拼接SQL语句字符串时的痛点问题". 一.If if标签可通 ...