求大神帮忙解决问题
- //分割图片
- public static void split(String str,String str2) throws Exception{
- FileInputStream fis = new FileInputStream(str);
- byte[] buf = new byte[1024*2];
- int ch = 0;
- int count = 1;
- while((ch = fis.read(buf)) != -1){
- String str3 =str2 + (count++)+ ".part";
- FileOutputStream fos = new FileOutputStream(str3);//存储碎片
- fos.write(buf, 0, ch);
- fos.close();
- }
- fis.close();
- }
- //查找当前目录下所有的图片".part",用ArrayList存储
- public static List<FileInputStream> findPig(String str) throws Exception{
- ArrayList<FileInputStream> list = new ArrayList<FileInputStream>();
- File f = new File(str);
- File[] files = f.listFiles();
-
- for(File file:files){
- if(file.getName().endsWith(".part")){
- list.add(new FileInputStream(file));
- }
- }
-
- return list;
- }
- //合并图片
- public static void merge(String str1,String str2) throws Exception{
-
- List<FileInputStream> list = new ArrayList<FileInputStream>();
-
- list = findPig(str1);//获得碎片集合
-
- final Iterator<FileInputStream> it = list.iterator();
- Enumeration<FileInputStream> em = new Enumeration<FileInputStream>(){
- public boolean hasMoreElements() {
-
- return it.hasNext();
- }
- public FileInputStream nextElement() {
-
- return it.next();
- }
- };
- SequenceInputStream sis = new SequenceInputStream(em);
- FileOutputStream fos = new FileOutputStream(str2);//存储合成图片
- byte[] buf = new byte[1024];
- int ch = 0;
- while((ch = sis.read(buf)) != -1){
- fos.write(buf, 0, ch);
- }
- fos.close();
- sis.close();
-
- }
复制代码
我的目的是先把图片切割成碎片,进行存储,然后对这些碎片合并,合并是成功了,但为什么合并后的图片打开是黑色的呢,而不是原图了呢?
|
|