1. 程式人生 > >spring 檔案上傳下載介面

spring 檔案上傳下載介面

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

//傳送介面

@RestController
public class FileUpload {

    static String url="http://******/aa";
    static HttpClient httpClient=HttpClientBuilder.create().build();
    @RequestMapping("/ccc")
    public static Object uploadFile(){
         File file = new File("D:\\abcd");//需要查詢的資料夾
         File[] files = file.listFiles(); //將路徑封裝成File陣列
            try {
                for (File file2 : files) {//遍歷這個陣列
                    String regex="(.*)jpg";
                    if (file2.isDirectory()) { //判斷是否是資料夾
//                    copyPhoto(file2);  //是的話就繼續呼叫這個方法
                    } else if (file2.getName().matches(regex)) { //不是的話就匹配是否是圖片
                      HttpPost post=new HttpPost(url);
                      HttpEntity entity=MultipartEntityBuilder.create()
                              .addBinaryBody("file", file2).build();
                      post.setEntity(entity);
                      HttpResponse httpResponse=httpClient.execute(post);
                      HttpEntity respEntity=httpResponse.getEntity();
                      String resuiltEntity=EntityUtils.toString(respEntity);
                      System.out.println(resuiltEntity);
                      //一下是備份檔案
                      BufferedInputStream bis=new BufferedInputStream(new FileInputStream(file2)) ;//建立輸入的管道          
                        byte[] buf = new byte[1024*20];//建立一個小陣列
                        int lenght = 0;
                        BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(  //建立輸出管道
                                "D:\\awdc\\" + file2.getName())); //圖片會拷貝到這裡
                        while ((lenght=bis.read(buf)) != -1) {
                            bos.write(buf, 0, lenght);
                        }
                        bos.close();
                        bis.close();
                      
                            file2.delete();//刪除指定檔案
                    }
                }
                    
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        return null;
    }
    

}

//接收介面

@RequestMapping("/aa")
    public String FilePicture(@RequestParam(value="file",required=false)
                                MultipartFile file ) {
        if (file!=null) {
            String fileName=file.getOriginalFilename();
            try {
                BufferedInputStream bis=new BufferedInputStream(file.getInputStream()) ;//建立輸入的管道          
                byte[] buf = new byte[1024*20];//建立一個小陣列
                int lenght = 0;
                BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(  //建立輸出管道
                        "D:\\awd\\" + fileName)); //圖片會拷貝到這裡
                while ((lenght=bis.read(buf)) != -1) {
                    bos.write(buf, 0, lenght);
                }
                bos.close();
                bis.close();
            } catch (Exception e) {
            }
              System.out.println("success");
         }
         
        return "200";
        
    }

//pom.xml

<dependency>
            <groupId>org.apache.myfaces.tobago</groupId>
            <artifactId>tobago-fileupload</artifactId>
            <version>2.0.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.5.6</version>
        </dependency>