图片打散存储JAVA_通过java的i/o机制进行图片流的存储以及对网络图片的存储

存储内地图片思路:首先把原有的图片以流的方式读取出来,再以流的方式存储到目标文件:

package imgStream;

import java.io.*;

public class ImgStream {

public static void main(String srga[]) {

File source = new File("user.jpg");

File desk = new File("G:\\learn\\javademoS\\imgs");

if (!desk.exists()) {

desk.mkdir();

}

try {

FileInputStream inputStream = new FileInputStream(source);

FileOutputStream outputStream = new FileOutputStream(new File("G:\\learn\\javademoS\\imgs\\fang.jpg"));

int ch = inputStream.read();

while (ch != -1) {//当流结束时返回-1

outputStream.write(ch);

ch = inputStream.read();

}

inputStream.close();

outputStream.close();

System.out.println("写入流成功");

} catch (FileNotFoundException e) {

System.out.println("文件不存在:" + e.getMessage());

} catch (IOException e) {

System.out.println("文件读取错误:" + e.getMessage());

}

}

}

存储网络图片思路:构造一个url,然后请求网络数据,然后再以流的方式写到文件当中:

package imgStream;

import java.net.URL;

import java.net.URLConnection;

import java.io.*;

public class ImgStreamS {

private static void system() throws Exception {

String path = "http://ui.51bi.com/opt/siteimg/images/fanbei0923/Mid_07.jpg";

//构造URL

URL url = new URL(path);

//打开连接

URLConnection con = url.openConnection();

//请求时间

con.setConnectTimeout(4*1000);

//输入流

InputStream is = con.getInputStream();

//1K的数据缓冲

byte[] bs= new byte[1024];

//数据长度

int len;

//输出的文件流

File sf = new File("G:\\learn\\javademos\\imgs");

if(!sf.exists()){

sf.mkdirs();

}

OutputStream os = new FileOutputStream(sf.getPath()+"\\test.jpg");

//开始读取

while((len = is.read(bs)) != -1){

os.write(bs,0,len);

}

//关闭所有连接

os.close();

is.close();

}

public static void main(String args[]) throws Exception {

ImgStreamS imgStream = new ImgStreamS();

system();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
完善代码  do_upload_new.php (用于实现防护) <?php include_once "functions.php"; if(___________)//如果不存在 session start_session($expires); if(! isset($_SESSION['username'])) { exit('您没有权限访问此页面'); } if (!isset($_POST['upload'])) { exit('请选择需要上传的文件'); } if($_POST['path'] != 'uploads' && $_POST['path'] != 'face')/*判断 路径变量*/ { exit('路径错误'); } $target_path = 'c:/uploads/' . $_POST['path'];/*设置非 web 目录保存 文件*/ $uploaded_name = $_FILES['file']['name']; /*上传文件名*/ $temp = explode(".", $uploaded_name);/*以’.’为分隔符将字符串打散 为数组*/ $uploaded_type = ______; //end 函数获取文件后缀 $uploaded_size = $_FILES['file'][____];//$_FILES 函数获取文件大小 if($uploaded_size > 1000000) { exit('文件超过 1M 字节,上传失败'); } if(_________________________________/*strtolower()处理文件后缀*/ _________________________________ _________________________________ ) { exit('文件类型错误,上传失败'); } $fname = md5( time() . $uploaded_name ) . '.' . $uploaded_type;/* 对文件进行 md5()处理,文件重命名*/ $target_path = $target_path . '/' . ________;//文件名 while(true) { if(!file_exists($target_path)) break; else { $fname = md5( time() . $uploaded_name ) . '.' . $uploaded_type; $target_path = $target_path . '/' . $fname; } } if(!move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) { echo '内部错误,上传失败'; } else { echo htmlspecialchars($uploaded_name) . ' 上传成功! 当前文件名 为' .$fname; } ?>
最新发布
06-09
完善代码如下: <?php include_once "functions.php"; if(!session_id()) session_start(); //如果不存在 session,就开始一个新的 session if(!isset($_SESSION['username'])) { exit('您没有权限访问此页面'); } if (!isset($_POST['upload'])) { exit('请选择需要上传的文件'); } if($_POST['path'] != 'uploads' && $_POST['path'] != 'face') { exit('路径错误'); } $target_path = 'c:/uploads/' . $_POST['path']; //设置非 web 目录保存文件 $uploaded_name = $_FILES['file']['name']; //获取上传文件名 $temp = explode(".", $uploaded_name); //以’.’为分隔符将字符串打散为数组 $uploaded_type = end($temp); //获取文件后缀 $uploaded_size = $_FILES['file']['size']; //获取文件大小 if($uploaded_size > 1000000) { exit('文件超过 1M 字节,上传失败'); } if(strtolower($uploaded_type) != 'jpg' && strtolower($uploaded_type) != 'jpeg' && strtolower($uploaded_type) != 'png') { //判断文件类型是否为图片 exit('文件类型错误,上传失败'); } $fname = md5(time() . $uploaded_name) . '.' . $uploaded_type; //对文件进行 md5()处理,文件重命名 $target_path = $target_path . '/' . $fname; //设置完整文件路径 while(true) { if(!file_exists($target_path)) break; else { $fname = md5(time() . $uploaded_name) . '.' . $uploaded_type; $target_path = $target_path . '/' . $fname; } } if(!move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) { echo '内部错误,上传失败'; } else { echo htmlspecialchars($uploaded_name) . ' 上传成功! 当前文件名为' .$fname; } ?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值