使用SpringBoot开发一个内网上传下载跨平台的工具,资源部署在Windows电脑上

这是基于SpringBoot开发的一个用于内网跨平台上传下载工具,安卓手机端、苹果平板、电脑只要连接在一个内网。资源部署在Windows电脑上,用浏览器实现上传下载。

上传下载必须借助浏览器,使用的是html的form表单上传下载
在这里插入图片描述

基于Maven开发

application.yml
在这里插入图片描述

server:
  port: 8029
spring:
  application:
    name: pFxServer
###ThymeLeaf配置 需要添加maven依赖才能生效
  thymeleaf:
    #模板的模式,支持 HTML, XML TEXT JAVASCRIPT
    mode: HTML5
    #编码 可不用配置
    encoding: UTF-8
    #内容类别,可不用配置
    #content-type: text/html
    #开发配置为false,避免修改模板还要重启服务器
    cache: false
    #配置模板路径,默认是templates,可以不用配置
    prefix: classpath:/templates
### Spring boot admin 监控
  #热部署
  devtools:
    restart:
      enabled: true

ResourcesConfig
在这里插入图片描述
/加载本地资源 registry.addResourceHandler("/server/**").addResourceLocations(“file:E:/webServer/”);

配置资源映射路径,访问/server/ 映射到E:/webServer/

package pfx.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
public class ResourcesConfig extends WebMvcConfigurationSupport{
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
 //加载本地资源     
 registry.addResourceHandler("/server/**").addResourceLocations("file:E:/webServer/");
       
        super.addResourceHandlers(registry);

    }
}

处理上传下载方法

package pfx.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

@Controller
public class GetResourcesController {

    @GetMapping(value = "/getAll")
    public String html(@RequestParam(value = "name",required = false)String s){
        return "/res.html";
    }

    @GetMapping(value = "/download")
    public String download(@RequestParam(value = "name",required = false)String s){

        return "/res.html";
    }
    //上传文件使用pot
    @PostMapping("/upload")
    @ResponseBody
    public String upload1(
            @RequestParam(value = "name",required = false)String name,
            @RequestParam(value = "path",required = false)String path,
            @RequestParam(value = "format",required = false)String format,
            @RequestParam("file") MultipartFile mFile){
        //String Dir = "E://";
        String dir = path;
        try {
            if (!mFile.isEmpty()){
                byte [] datas = mFile.getBytes();
                //如果是小文件,则直接存储到文件
                mFile.transferTo(new File(dir+name+format));
            }
            //如果是大文件,则使用 mFile. getInputStream()
            //返回实时的上传进度
//
//            //输出节点流,如果文件不存在则自动创建
//            FileOutputStream fos = new FileOutputStream("source/上传测试.txt");
//            //缓冲输出流
//            BufferedOutputStream bos = new BufferedOutputStream(fos);
//            for (byte data: datas
//                 ) {
//                bos.write(data);
//            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        return "ok";

    }

    //实现Spring Boot 的文件下载功能,映射请求为/download
    @PostMapping("/download")
    public void getFiles(@RequestParam(value = "name",required = false)String name,
                         @RequestParam(value = "path",required = false)String path,
               HttpServletResponse response) {
        //String fileDir ="E://";
        String fileDir = path;
        File downloadFile = new File(fileDir + name);
            // 如果文件名存在,则进行下载
            if (downloadFile.exists()) {
                // 配置文件下载
                response.setHeader("content-type", "application/octet-stream");
                response.setContentType("application/octet-stream");
                // 下载文件能正常显示中文
                try {
                    response.setHeader("Content-Disposition",
                            "attachment;filename=" +
                                    URLEncoder.encode(name, "UTF-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

                // 实现文件下载
                byte[] buffer = new byte[1024];
                try (FileInputStream fis = new FileInputStream(downloadFile);
                     BufferedInputStream bis = new BufferedInputStream(fis);
                     OutputStream os = response.getOutputStream();
                ) {
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                   
                } catch (Exception e) {
                   e.printStackTrace();
                }
            }
    }


}

在这里插入图片描述
启动微服务
在这里插入图片描述
访问http://localhost:8029/getAll

 @GetMapping(value = "/getAll")
    public String html(@RequestParam(value = "name",required = false)String s){
        return "/res.html";
    }

在这里插入图片描述

文件上传

选择文件
在这里插入图片描述
在这里插入图片描述
点击上传
在这里插入图片描述
上传接口

//上传文件使用pot
    @PostMapping("/upload")
    @ResponseBody
    public String upload1(
            @RequestParam(value = "name",required = false)String name,
            @RequestParam(value = "path",required = false)String path,
            @RequestParam(value = "format",required = false)String format,
            @RequestParam("file") MultipartFile mFile){
        //String Dir = "E://";
        String dir = path;
        try {
            if (!mFile.isEmpty()){
                byte [] datas = mFile.getBytes();
                //如果是小文件,则直接存储到文件
                mFile.transferTo(new File(dir+name+format));
            }
            //如果是大文件,则使用 mFile. getInputStream()
            //返回实时的上传进度
//
//            //输出节点流,如果文件不存在则自动创建
//            FileOutputStream fos = new FileOutputStream("source/上传测试.txt");
//            //缓冲输出流
//            BufferedOutputStream bos = new BufferedOutputStream(fos);
//            for (byte data: datas
//                 ) {
//                bos.write(data);
//            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        return "ok";

    }

上传的form表单

<form method="post" action="/upload" enctype="multipart/form-data">
			<fieldset>
				<h3 class="hdr-account">选择文件</h3>
				<input type="file" name="file" />

			</fieldset>

			<fieldset>
				<h3 class="hdr-address">输入文件名</h3>
				<input type="text" name="name" />
			</fieldset>

			<fieldset>
				<h3 class="hdr-address">上传路径</h3>
				<input type="text" name="path" list="pathList" />
				<datalist id="pathList">
                   <option>E://</option>
                   <option>F://</option>
                   <option>D://</option>
                   <option>C://</option>
                 </datalist>
			</fieldset>

			<fieldset>
				<h3 class="hdr-address">文件格式</h3>
				<input type="text" name="format" list="formatList">
				<datalist id="formatList">
                   <option>.txt</option>
                   <option>.jpg</option>
                   <option>.pdf</option>
                   <option>.zip</option>
                   </datalist>
				<input type="submit" value="上传">
			</fieldset>

		</form>

文件上传到E盘的根目录下
在这里插入图片描述

文件下载

选择路径,输入文件名 点击下载就会下载到本地
在这里插入图片描述
下载接口

 //实现Spring Boot 的文件下载功能,映射请求为/download
    @PostMapping("/download")
    public void getFiles(@RequestParam(value = "name",required = false)String name,
                         @RequestParam(value = "path",required = false)String path,
               HttpServletResponse response) {
        //String fileDir ="E://";
        String fileDir = path;
        File downloadFile = new File(fileDir + name);
            // 如果文件名存在,则进行下载
            if (downloadFile.exists()) {
                // 配置文件下载
                response.setHeader("content-type", "application/octet-stream");
                response.setContentType("application/octet-stream");
                // 下载文件能正常显示中文
                try {
                    response.setHeader("Content-Disposition",
                            "attachment;filename=" +
                                    URLEncoder.encode(name, "UTF-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

                // 实现文件下载
                byte[] buffer = new byte[1024];
                try (FileInputStream fis = new FileInputStream(downloadFile);
                     BufferedInputStream bis = new BufferedInputStream(fis);
                     OutputStream os = response.getOutputStream();
                ) {
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }

                } catch (Exception e) {
                   e.printStackTrace();
                }
            }
    }

form表单

<form method="post" action="/download">
			<div class="row">
				<fieldset>
					<fieldset>
						<h3 class="hdr-address">输入文件名</h3>
						<input type="text" name="name" />
					</fieldset>

					<fieldset>
						<h3 class="hdr-address">下载路径</h3>
						<input type="text" name="path" list="pathList2" />
						<datalist id="pathList2">
                          <option>E://</option>
                          <option>F://</option>
                          <option>D://</option>
                          <option>C://</option>
                       </datalist>
						<input type="submit" value="下载">
					</fieldset>
				</fieldset>
			</div>
		</form>

html

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8"/>
    <title></title>
    <link rel="stylesheet" type="text/css" href="css/form1.css" title="Style">

</head>

<body>
<h2>上传文件</h2>
<form method="post" action="/upload" enctype="multipart/form-data">
    <fieldset>
        <h3 class="hdr-account">选择文件</h3>
        <input type="file" name="file"/>

    </fieldset>

    <fieldset>
        <h3 class="hdr-address">输入文件名</h3>
        <input type="text" name="name"/>
    </fieldset>

    <fieldset>
        <h3 class="hdr-address">上传路径</h3>
        <input type="text" name="path" list="pathList"/>
        <datalist id="pathList">
            <option>E://</option>
            <option>F://</option>
            <option>D://</option>
            <option>C://</option>
        </datalist>
    </fieldset>

    <fieldset>
        <h3 class="hdr-address">文件格式</h3>
        <input type="text" name="format" list="formatList">
        <datalist id="formatList">
            <option>.txt</option>
            <option>.jpg</option>
            <option>.pdf</option>
            <option>.zip</option>
        </datalist>
        <input type="submit" value="上传">
    </fieldset>

</form>

<h2>下载文件</h2>
<form method="post" action="/download">
    <div class="row">
        <fieldset>
            <fieldset>
                <h3 class="hdr-address">输入文件名</h3>
                <input type="text" name="name"/>
            </fieldset>

            <fieldset>
                <h3 class="hdr-address">下载路径</h3>
                <input type="text" name="path" list="pathList2"/>
                <datalist id="pathList2">
                    <option>E://</option>
                    <option>F://</option>
                    <option>D://</option>
                    <option>C://</option>
                </datalist>
                <input type="submit" value="下载">
            </fieldset>
        </fieldset>
    </div>
</form>

</body>

</html>

<!--                                        <fieldset>-->
<!--                                            <h2 class="hdr-public-profile">其他</h2>-->
<!--                                           其他-->
<!--                                            <div class="row">-->
<!--                                                <fieldset class="radios">-->
<!--                                                    <legend>Gender:</legend>-->
<!--                                                    <input type="radio" id="gender-male" name="gender" value="male" />-->
<!--                                                    <label for="gender-male">Male</label>-->
<!--                                                    <input type="radio" id="gender-female" name="gender" value="female" />-->
<!--                                                    <label for="gender-female">Female</label>-->
<!--                                                </fieldset>-->
<!--                                            </div>-->
<!--                                        </fieldset>-->

css

form1.css

fieldset {
    background-color: #f1f1f1;
    border: none;
    border-radius: 2px;
    margin-bottom: 12px;
    overflow: hidden;
    padding: 0 .625em; /* 10px */
}
.fields {
    background-color: #fff;
    border: 1px solid #eaeaea;
    margin: .75em; /* 12px */
    padding: .75em;
}
.fields .row {
    margin: 0.5em 0;
}
label {
    cursor: pointer;
    display: inline-block;
    padding: 3px 6px;
    text-align: right;
    width: 150px;
    vertical-align: top;
}
input, select, button {
    font-size: inherit;
}
/*  各种表单字段的宽度 */
.field-small {
    width: 75px;
}
.field-medium {
    width: 150px;
}
.field-large {
    width: 250px;
}

这里只能单文件上传和下载
maven package打包成可运行jar,电脑必须安装jdk环境才能运行!!!
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值