<div class="layui-form-item"> <label class="layui-form-label">图标</label> <div class="layui-input-inline"> <div class="layui-upload"> <button type="button" class="layui-btn" id="img_b"><i class="layui-icon"></i>上传</button> <div class="layui-upload-list"> <img class="layui-upload-img" id="img_view" style="width: 200px;height: 100%;"> <input hidden id="img_v" name="bank_icon"/> <p id="test-upload-demoText"></p> </div> </div> </div> </div>
upload.render({ elem: '#img_b' , url: setter.live_domain + '/upload/oss?path=bankList' , method: 'post' , data: { type: 'image' } , accept: 'file', before: function (obj) { //预读本地文件示例,不支持ie8 obj.preview(function (index, file, result) { $('#img_view').attr('src', result); //图片链接(base64) }); }, done: function (res) { $('#img_v').val(res.url); $('#img_v').val(res.data); layer.msg('上传成功'); }, error: function () { layer.msg('上传失败'); } });
public String upload( @RequestParam( "file" ) MultipartFile file, @RequestParam( "path" ) String path ) throws IOException {
String fileName = file.getOriginalFilename();
String fileKey = path + "/" + fileName;
InputStream inputStream =file.getInputStream()
ServerOss serverOss = this.effect();
// 创建OSSClient实例
OSS ossClient = new OSSClientBuilder().build( serverOss.getEndpoint(), serverOss.getAccessKey(),serverOss.getAccessSecret() );
// 上传文件流
ossClient.putObject( serverOss.getBucket(), fileKey, inputStream );
// 关闭client
ossClient.shutdown();
return "https://" + serverOss.getVhost() + "/" + fileKey;
}
serverOss里的字段
/** * oss 服务key */ @TableField private String accessKey; /** * oss 服务密钥 */ @TableField private String accessSecret; /** * 访问域名 */ @TableField private String endpoint; /** * 文件存储 */ @TableField private String bucket; /** * 加速域名 */ @TableField private String vhost;