图片上传类

<?php


class image {

    /**
     *完成图片的上传
     *
     *@param array $file 待上传的文件信息的数组,用于5个元素的那个数组
     *@return mixed 如果执行成功,返回上传了的文件名,否则返回false
     */
    public function upload($file) {

        if($file['error'] == 0) {
            $allow_types = array('image/jpeg', 'image/pjpeg', 'image/png', 'image/gif');
            if(in_array($file['type'], $allow_types)) {
                $maxsize = 2000000;
                if($file['size'] <= $maxsize) {
                    //上传
                    //需要将文件重命名,1,防止不规则的字符出现在文件名中,2,防止重名
                    //采用时间戳加随机数的形式
                    //后缀名如何获得?在原始文件名中获得后缀名
                    //在文件名中最后一个点截取到最后就是扩展名
                    //strrchr(在哪个字符串中查,查的字符串);
                    $new_filename = time() . mt_rand(10000, 99999) . strrchr($file['name'], '.');

                    //移动
                    //此函数返回移动成功还是失败
                    if(move_uploaded_file($file['tmp_name'], ROOT_PATH . 'upload/'. $new_filename)) {
                        return $new_filename;
                    }
                }
            }
        }

        //只有一种情况返回文件名,其他全部返回false
        return false;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个 Java 图片上传的工具示例: ```java import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; public class ImageUploader { private static final int BUFFER_SIZE = 4096; public static void uploadImage(String imageUrl, String saveDir) throws IOException { URL url = new URL(imageUrl); HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); int responseCode = httpConn.getResponseCode(); // 检查请求是否成功 if (responseCode == HttpURLConnection.HTTP_OK) { String fileName = ""; String disposition = httpConn.getHeaderField("Content-Disposition"); String contentType = httpConn.getContentType(); int contentLength = httpConn.getContentLength(); if (disposition != null) { // 从 Content-Disposition 中获取文件名 int index = disposition.indexOf("filename="); if (index > 0) { fileName = disposition.substring(index + 10, disposition.length() - 1); } } else { // 从 URL 中获取文件名 fileName = imageUrl.substring(imageUrl.lastIndexOf("/") + 1, imageUrl.length()); } // 打印文件信息 System.out.println("Content-Type = " + contentType); System.out.println("Content-Disposition = " + disposition); System.out.println("Content-Length = " + contentLength); System.out.println("fileName = " + fileName); // 从连接中读取数据流并保存到本地文件 InputStream inputStream = httpConn.getInputStream(); String saveFilePath = saveDir + File.separator + fileName; OutputStream outputStream = new FileOutputStream(saveFilePath); int bytesRead = -1; byte[] buffer = new byte[BUFFER_SIZE]; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } outputStream.close(); inputStream.close(); System.out.println("文件 " + fileName + " 上传成功"); } else { System.out.println("无法获取文件信息,响应码为 " + responseCode); } httpConn.disconnect(); } } ``` 使用方式: ```java public static void main(String[] args) throws IOException { String imageUrl = "https://example.com/image.jpg"; String saveDir = "/path/to/save/dir"; ImageUploader.uploadImage(imageUrl, saveDir); } ``` 其中 `imageUrl` 是图片的 URL 地址,`saveDir` 是保存图片的目录路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值