开发者社区> 问答> 正文

java上传到oss,图片名字虽然不一样,但都是同一张图片

package life.majiang.community.community.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.PutObjectRequest;
import life.majiang.community.community.dto.FileDTO;
import life.majiang.community.community.util.AliyunOssConfigConstant;
import org.springframework.stereotype.Controller;
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.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.UUID;

/**
 * 处理文件上传
 */
@Controller
public class FileController {
    private static String bucketName = AliyunOssConfigConstant.BUCKE_NAME;
    private static String endPoint = AliyunOssConfigConstant.END_POINT;
    private static String accessKeyId = AliyunOssConfigConstant.AccessKey_ID;
    private static String accessKeySecret = AliyunOssConfigConstant.AccessKey_Secret;
    private static String fileHost = AliyunOssConfigConstant.FILE_HOST;

    @ResponseBody
    @RequestMapping("/file/upload")
    public FileDTO upload(HttpServletRequest request){
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)request;
        MultipartFile file = multipartRequest.getFile("editormd-image-file");
        String realPath = System.getProperty("user.dir");
        realPath+="\\target\\classes\\static\\images\\";//uploadFile
        System.out.println(realPath);
        File folder = new File(realPath);
        if(!folder.isDirectory()){
            //文件夹不存在,创建
            folder.mkdirs();
        }

        OSS ossClient = new OSSClientBuilder().build(endPoint,accessKeyId ,accessKeySecret);
        //文件重命名
        String oldFile = file.getOriginalFilename();
        String newFile = UUID.randomUUID().toString()+oldFile.substring(oldFile.lastIndexOf("."),oldFile.length());
        String url;
        FileDTO fileDTO = null;
        try {
            //保存文件
            file.transferTo(new File(folder,newFile));

            //拼接url
            url = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+"/images/"+newFile;
            fileDTO = new FileDTO();
            fileDTO.setSuccess(1);
            fileDTO.setUrl(url);
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, url, new File("/images/"));
            ossClient.putObject(putObjectRequest);
            return fileDTO;

        }catch (Exception e){
            fileDTO.setSuccess(0);
            e.printStackTrace();
        }finally {
            ossClient.shutdown();
        }
        return null;
    }


}



上传到oss名字虽然不一样,但都是同一张图片。 java保存到文件图片没问题,就是上传到oss的图片都是同一张。

展开
收起
游客w75gc4khveq2m 2020-06-10 22:51:05 1567 0
1 条回答
写回答
取消 提交回答
  • 精于基础,广于工具,熟于业务。

    是不是的你的程序自己处理l了。给图片改名字了

    2020-06-12 17:36:35
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
OSS运维进阶实战手册 立即下载
《OSS运维基础实战手册》 立即下载
OSS运维基础实战手册 立即下载