使用SpringBoot将手机中的照片上传到电脑

28 篇文章 2 订阅
18 篇文章 0 订阅

苹果手机如果想要将手机相册中照片上传到笔记本或电脑上,需要下载iTunes,需要连接到电脑,不太好操作;作为一个程序员,将照片上传到个人电脑其实很简单,

本文介绍使用SpringBoot开发一个简单的应用即可将手机上的照片上传到电脑。

 

1、开发SpringBoot应用:图片上传功能

1.1 添加maven依赖:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- servlet 依赖. -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

        <!-- tomcat 的支持. -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>

    </dependencies>

 

1.2 配置文件

由于涉及上传页面,需要使用JSP(SpringBoot整合JSP可以参考博文:SpringBoot入门教程四:Spring Boot添加JSP支持),新增配置文件application.properties, 内容如下:

server.port=8080

# 页面默认前缀目录
spring.mvc.view.prefix=/WEB-INF/jsp/
# 响应页面默认后缀
spring.mvc.view.suffix=.jsp

# 上传文件的最大值
spring.servlet.multipart.max-file-size=1000MB
spring.servlet.multipart.max-request-size=1000MB

注意:

springboot 默认 multipart.max-file-size大小是1M,max-request-size默认大小是10M,现在手机的像素都比较高,拍的照片都比较大,默认的基本不够,可根据个人情况适当调整。

 

1.3 编写上传页面

在webapp文件夹(如果没有,在main文件夹下创建)下新增文件夹 :WEB-INF/jsp/,在jsp文件夹下新增index.jsp, 内容如下:

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title>图片上传</title>
</head>
<body>
<form action="/upload" method="post" enctype="multipart/form-data">
    <input type="file" name="file" accept=".png,.jpg" multiple/>
    <button type="submit">点我上传</button>
</form>
</body>
</html>

 

1.4 编写首页及上传图片接口

首页:IndexController:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class IndexController {

    @GetMapping("index")
    public String index() {
        return "index";
    }
}

上传接口:

import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
public class FileUploadController {

    @RequestMapping(value = "upload", method = RequestMethod.POST)
    public String fileUplaod(@RequestParam(value = "file") MultipartFile[] files) throws IOException {
        for (int i = 0; i < files.length; i++) {
            MultipartFile file = files[i];
            byte[] bytes = file.getBytes();
            String pathName = "D:\\pictures\\" + file.getOriginalFilename();
            File destFile = new File(pathName);
            FileCopyUtils.copy(bytes, destFile);
        }
        return "success";
    }
}

 

1.5 编写启动类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class PicUploadApplication {

    public static void main(String[] args) {
        SpringApplication.run(PicUploadApplication.class, args);
    }
}

至此,上传图片功能的 SpringBoot 应用开发完成, 运行PicUploadApplication ,启动成功后,先使用电脑浏览器访问:http://127.0.0.1:8080/index

文件会上传到电脑的D:/pictures 文件夹下, 上传前确保D盘下已经创建了pictures目录,支持多个图片上传。

 

2、使用手机浏览器访问

 

以上已经可以访问及上传图片了,现在我们使用手机浏览器来访问;

首先确保手机和运行SpringBoot应用的电脑在同一wifi或局域网下;

然后查看本机的ip地址,我的电脑ip为:192.168.0.102。

测试手机为iphone8,打开浏览器,访问:http://192.168.0.102:8080/index,效果如下:(如果访问不了,请参考博文: 同一wifi或局域网下手机访问windows10电脑 解决。)

点击选取文件,弹出选择菜单:

选择照片图库,就可以选择手机上的照片,选择好后,点击 ‘点我上传’, 即可将手机上的照片上传到 电脑的 D:/pictures 目录下。

手机上存了好久的照片几分钟就全部传到了电脑上了,有兴趣的赶快去试试吧!

源码: https://github.com/river106/file-transfer

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值