Skip to content

前端需要这样:

f15d27bf3bb9cb78e10790db0f71556a_MD5 需要制定post方法.这是因为文件要放到内容当中 enctype也是写死的.(否则提交时不会提交内容,只会提交图片的名字.) 图像上传的表单项中type要写File 这称为页面三要素

服务端:

0a1c68d11634e5fe0a5a4fcfd7b84785_MD5 注意形参名也要写成file,这是和name对应的.否则需要requestParam注解了

测试时:

f817b661220d2fbda23f610622d7c541_MD5 然后访问8080/upload.html即可.

本地存储

28f19a4937e7d48c2ce729afecf7cff7_MD5 这个方法可以存到一个路径当中.存储的时候可以使用uuid. 65fb434a4fc4d6b1aed8a8b4c41a2e2c_MD5 apifox可以帮我们测试: 6ac8dde26e94c9b971b26da31b5a664f_MD5 注意,Spring默认文件上传大小为1M.如果超过了,那么就会报错.可以配置: b2f22187c96ea16eebce823d79ac161d_MD5

阿里云OSS(当然,云存储其实可以自己搭建的)

49a62e846c886cbc75d908d186acbdae_MD5 注意,启动accesskey需要管理员本地设置环境变量:

set OSS_ACCESS_KEY_ID=XXXXX
set OSS_ACCESS_KEY_SECRET=XXXX
然后让修改生效:
setx OSS_ACCESS_KEY_ID "%OSS_ACCESS_KEY_ID%"
setx OSS_ACCESS_KEY_SECRET "%OSS_ACCESS_KEY_SECRET%"
验证是否生效:
echo %OSS_ACCESS_KEY_ID%
echo %OSS_ACCESS_KEY_SECRET%
给个样例程序吧:
package cn.iamwsll;

import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
import com.aliyun.oss.common.comm.SignVersion;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.nio.file.Files;

public class TestOss {

    public static void main(String[] args) throws Exception {
        // Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
        String endpoint = "https://oss-cn-beijing.aliyuncs.com";
        // 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // 填写Bucket名称,例如examplebucket。
        String bucketName = "iamwsll-javatest1";
        // 填写Object完整路径,例如exampledir/exampleobject.txt。Object完整路径中不能包含Bucket名称。(注:这其实就是上传到oss当中的名称)
        String objectName = "001.jpg";
        // 填写Bucket所在地域。以华东1(杭州)为例,Region填写为cn-hangzhou。
        String region = "cn-beijing";

        // 创建OSSClient实例。
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
        OSS ossClient = OSSClientBuilder.create()
                .endpoint(endpoint)
                .credentialsProvider(credentialsProvider)
                .clientConfiguration(clientBuilderConfiguration)
                .region(region)
                .build();

        try {
            File file = new File("C:\\photos\\动漫\\wallhaven-z8ek1y.jpg");
            byte[] content = Files.readAllBytes(file.toPath());

            ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(content));
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}

然后可以看到项目的4 5部分.