Java上传图片到本地

上传到指定的路径,并以当前的日期分类

public String add(MultipartFile file) throws Exception {
        String path = null;// 文件路径

        if (file != null) {// 判断上传的文件是否为空
            String type = null;// 文件类型
            String fileName = file.getOriginalFilename();// 文件原名称
            System.out.println("上传的文件原名称:" + fileName);

            // 判断文件类型
            type = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()) : null;
            if (type != null) {// 判断文件类型是否为空

                if ("GIF".equals(type.toUpperCase()) || "PNG".equals(type.toUpperCase()) || "JPG".equals(type.toUpperCase())) {

                    String uuid = UUID.randomUUID().toString().replaceAll("-","");
                    fileName = uuid + "."+type;
                    System.out.println("文件名称:"+fileName);

                    // 添加日期
                    String datePath = new DateTime().toString("yyyy/MM/dd");
                    path = "C:/Users/MD/Desktop/Upload/"+datePath+"/";
                    File f = new File(path);
                    if (!f.exists()){
                        f.mkdirs();
                    }

                    String pathName = path + fileName;
                    System.out.println("存放图片文件的路径:" + pathName);

                    // 转存文件到指定的路径
                    file.transferTo(new File(pathName));

                    System.out.println("文件成功上传到指定目录下");
                    return datePath + "/" + fileName + "." + type;
                }

            } else {
                return "不是我们想要的文件类型,请按要求重新上传";
            }
        } else {
            return "文件类型为空";
        }
        return "已经成功上传到指定目录";

    }
  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
中,需要使用java.io.File类和java.net.URL类来完成。具体步骤如下: 1. 创建一个File对象,指定本地图片所在的路径和文件名。 ``` File file = new File("C:/pictures/test.jpg"); ``` 2. 创建一个URL对象,指定系统中存储图片的地址。 ``` URL url = new URL("http://localhost:8080/upload"); ``` 3. 创建一个HttpURLConnection对象,用于上传图片。 ``` HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setRequestMethod("POST"); ``` 4. 创建一个DataOutputStream对象,用于向服务器发送数据。 ``` DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream()); ``` 5. 使用FileInputStream读取本地图片文件,并将其写入DataOutputStream中。 ``` FileInputStream inputStream = new FileInputStream(file); byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, length); } inputStream.close(); ``` 6. 关闭DataOutputStream并获取服务器返回的结果。 ``` outputStream.flush(); outputStream.close(); int responseCode = connection.getResponseCode(); ``` 完整代码示例: ``` import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.net.HttpURLConnection; import java.net.URL; public class UploadImage { public static void main(String[] args) { try { File file = new File("C:/pictures/test.jpg"); URL url = new URL("http://localhost:8080/upload"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setRequestMethod("POST"); DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream()); FileInputStream inputStream = new FileInputStream(file); byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, length); } inputStream.close(); outputStream.flush(); outputStream.close(); int responseCode = connection.getResponseCode(); System.out.println("Server response code: " + responseCode); } catch (Exception e) { e.printStackTrace(); } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值