자바 URL 로 이미지 다운로드 - 간단 한 구현
                                            
 2007 단어  JAVA 기초
                    
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
@Api(tags = "    ")
@RestController
@RequestMapping("/v1/bestore/services")
@Validated
public class TestDownLoadRest {
    @RequestMapping(value = {"/download"}, produces = "application/json", method = RequestMethod.GET)
    @ApiOperation(value = "    ,urlList   URL  ,path          D://")
    public void download(@RequestParam("urlList") String urlList,@RequestParam("path") String path) {
        URL url = null;
        try {
            url = new URL(urlList);
            //     
            URLConnection con = url.openConnection();
            //       5s
            con.setConnectTimeout(5*1000);
            //    
            InputStream is = con.getInputStream();
            // 1K     
            byte[] bs = new byte[1024];
            //         
            int len;
            //       
            File sf=new File(path);
            if(!sf.exists()){
                sf.mkdirs();
            }
            //        ,    
            OutputStream os = new FileOutputStream(sf.getPath()+"\\"+System.currentTimeMillis()+".png");
            //     
            while ((len = is.read(bs)) != -1) {
                //    
                os.write(bs, 0, len);
            }
            //   ,      
            os.close();
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
자바 로 데이터베이스 시트 에 따라 실체 클래스 생 성텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.