精华内容
下载资源
问答
  • java 保存图片

    2021-02-12 09:13:57
    使用BufferedImage生成图片保存图片有两种方式:1.JPEGImageEncoder该种方式保存的图片会失真,而且图片还大,不建议使用;BufferedImage image;BufferedOutputStream bos = null;if (image != null) {try {...

    使用BufferedImage生成图片

    保存图片有两种方式:

    1.JPEGImageEncoder

    该种方式保存的图片会失真,而且图片还大,不建议使用;

    BufferedImage image;

    BufferedOutputStream bos = null;

    if (image != null) {

    try {

    FileOutputStream fos = new FileOutputStream("E:\\testPic.png");

    bos = new BufferedOutputStream(fos);

    // image.getGraphics().drawImage(

    // image.getScaledInstance(imageWidth, imageHeight,java.awt.Image.SCALE_SMOOTH), 0, 0, null);

    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);

    encoder.encode(image);

    bos.close();

    } catch (Exception e) {

    e.printStackTrace();

    } finally {

    if (bos != null) {// 关闭输出流

    try {

    bos.close();

    } catch (IOException e) {

    e.printStackTrace();

    }

    }

    }

    }

    b28396334150

    JPEGImageEncoder 图片效果

    2.使用ImageIO保存图片

    推荐使用,图片精度高,size小;

    // 得到图形对象

    Graphics2D g2d = image.createGraphics();

    // 将窗口内容面板输出到图形对象中

    // content.printAll(g2d);

    // 保存为图片

    File f = new File("E:\\testPic.png");

    try {

    ImageIO.write(image, "png", f);

    } catch (IOException e) {

    e.printStackTrace();

    }

    // 释放图形对象

    g2d.dispose();

    b28396334150

    ImageIO 图片效果

    展开全文
  • 图片路径下载版本: public String saveImg(String img_url, String savename) { InputStream input = null; OutputStream os = null; try { URL url = new URL(img_url); // 打开URL连接 URLConnection ...

    图片路径下载版本:

          public String saveImg(String img_url, String savename) {
            InputStream input = null;
            OutputStream os = null;
    
            try {
                URL url = new URL(img_url);
                // 打开URL连接
                URLConnection con = url.openConnection();
                // 得到URL的输入流
                input = con.getInputStream();
                // 设置数据缓冲
                byte[] bs = new byte[1024 * 2];
                // 读取到的数据长度
                int len;
                // 输出的文件流保存图片至本地
                os = new FileOutputStream(aLiYunConfig.path + savename);
                while ((len = input.read(bs)) != -1) {
                    os.write(bs, 0, len);
                }
            } catch (Exception e) {
                logger.error("图片保存失败:{}", e.getMessage());
            } finally {
                try {
                    os.close();
                    input.close();
                } catch (Exception e) {
    
                    logger.error("数据流关闭失败:{}", e.getMessage());
                }
            }
            return "img/" + savename;
        }

    Base64图片保存(base64删掉头部data:?base64,不然保存图片会有问题)

    public String saveImg(String imgstr, String savename) {
            BufferedImage image = null;
            byte[] imageByte = null;
            try {
                imageByte = DatatypeConverter.parseBase64Binary(imgstr);
                image = ImageIO.read(new ByteArrayInputStream(imageByte));
                File outpuFile = new File(aLiYunConfig.path + savename);
                ImageIO.write(image,"jpg",outpuFile);
            } catch (Exception e) {
                logger.error("图片保存失败:{}", e.getMessage());
                return null;
            }
            return "img/" + savename;
        }

     

    展开全文
  • 使用Java保存风景图片

    2021-03-13 05:57:18
    1.下载依赖 xsi:schemaLocation=... } } } 标签:Java,String,http,保存,风景图片,org,apache,import,com 来源: https://www.cnblogs.com/csj2018/p/12389858.html

    1.下载依赖

    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    4.0.0

    com.feiyangedu.sample

    hellodep

    0.0.1-SNAPSHOT

    jar

    UTF-8

    1.8

    1.8

    1.8

    javax.mail

    mail

    1.4

    com.fasterxml.jackson.core

    jackson-databind

    2.9.0

    com.fasterxml.jackson.datatype

    jackson-datatype-jsr310

    2.9.0

    mysql

    mysql-connector-java

    8.0.17

    com.zaxxer

    HikariCP

    3.3.1

    org.apache.poi

    poi

    3.17

    org.apache.poi

    poi-ooxml

    3.17

    org.testng

    testng

    6.14.3

    compile

    io.rest-assured

    rest-assured

    4.1.1

    compile

    org.hamcrest

    hamcrest-all

    1.3

    test

    org.hamcrest

    hamcrest-core

    1.3

    test

    org.apache.httpcomponents

    httpclient

    4.5.8

    org.jsoup

    jsoup

    1.10.3

    2.解析图片地址

    https://www.ivsky.com/tupian/shenqiu_luoye_v58110/

    c3d2ff60c2bd1a3f3b77fe82248277cb.png

    确定图片元素路径,以用于解析。

    注意需要将//img.ivsky.com/img/tupian/t/201909/09/shenqiu_luoye.jpg替换为pre。因为t是缩略图,而pre是原图。

    3.示例

    package com.testerhome;

    import io.restassured.response.Response;

    import org.apache.http.HttpConnection;

    import org.apache.http.HttpEntity;

    import org.apache.http.HttpStatus;

    import org.apache.http.client.ClientProtocolException;

    import org.apache.http.client.methods.CloseableHttpResponse;

    import org.apache.http.client.methods.HttpGet;

    import org.apache.http.impl.client.CloseableHttpClient;

    import org.apache.http.impl.client.HttpClients;

    import org.apache.http.util.EntityUtils;

    import org.jsoup.Jsoup;

    import org.jsoup.nodes.Document;

    import org.jsoup.nodes.Element;

    import org.jsoup.select.Elements;

    import org.testng.annotations.Test;

    import java.io.*;

    import java.net.HttpURLConnection;

    import java.net.MalformedURLException;

    import java.net.URL;

    import java.util.ArrayList;

    import static io.restassured.RestAssured.given;

    import static org.hamcrest.MatcherAssert.assertThat;

    import static org.hamcrest.Matchers.greaterThan;

    public class GetScenery {

    @Test

    public void getAutumn(){

    String des = "autumn";

    String url = "https://www.ivsky.com/tupian/shenqiu_luoye_v58110/";

    File desDir = new File(des);

    if(!desDir.exists() || !desDir.isDirectory()){

    desDir.mkdir();

    }

    ArrayList imgUrls = getPic(url);

    for(int i=0;i

    downPic(imgUrls.get(i),des);

    System.out.println(imgUrls.get(i));

    }

    }

    public ArrayList getPic(String url){

    ArrayList result = new ArrayList<>();

    String select = "body > div:nth-child(4) > div.left > ul";

    CloseableHttpClient httpClient = HttpClients.createDefault();

    CloseableHttpResponse response ;

    HttpGet request = new HttpGet(url);

    request.setHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36");

    try{

    response = httpClient.execute(request);

    HttpEntity httpEntity = response.getEntity();

    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

    String html = EntityUtils.toString(httpEntity, "gbk");

    Document document = Jsoup.parse(html);

    Elements pics = document.select("div.left>ul.pli>li");

    for(Element pic:pics){

    Elements img = pic.select("div.il_img>a>img");

    String imgUrl = "http:"+img.attr("src");

    //t是缩略图,pre是大图

    result.add(imgUrl.replaceAll(".?t/","/pre/"));

    }

    }

    }catch (ClientProtocolException e){

    e.printStackTrace();

    }catch (IOException e){

    e.printStackTrace();

    }

    return result;

    }

    public void downPic(String url,String dir){

    String fileName = dir + url.substring(url.lastIndexOf("/"));

    try {

    FileOutputStream fop = new FileOutputStream(fileName);

    URL imgUrl = new URL(url);

    HttpURLConnection conn = (HttpURLConnection)imgUrl.openConnection();

    InputStream fip = conn.getInputStream();

    File file = new File(fileName);

    byte[] buffer = new byte[2048];

    for(;;){

    int hasRead = fip.read(buffer);

    if(hasRead <0){

    break;

    }

    fop.write(buffer,0,hasRead);

    }

    }catch (Exception e){

    e.printStackTrace();

    }

    }

    }

    27bdbd5d2ea417f1da84bf0c33aaa863.png

    标签:Java,String,http,保存,风景图片,org,apache,import,com

    来源: https://www.cnblogs.com/csj2018/p/12389858.html

    展开全文
  • } // 查看图片 public String showImg() { HttpServletResponse response = ServletActionContext.getResponse(); WorkFlow wf = ws.findById(wfId); try { response.getOutputStream().write(wf.getProcessImg());...

    // 文件转换为字节数组

    private byte[] getByteFromFile(File file) {

    byte[] be = null;

    try {

    if (file == null) {

    return null;

    }

    FileInputStream in = new FileInputStream(file);

    ByteArrayOutputStream out = new ByteArrayOutputStream(4000);

    byte[] b = new byte[4000];

    int n;

    while ((n = in.read(b)) != -1) {

    out.write(b, 0, n);

    }

    in.close();

    out.close();

    be = out.toByteArray();

    } catch (Exception e) {

    e.printStackTrace();

    }

    return be;

    }

    // 查看文件

    public String execute() throws IOException, DocumentException {

    HttpServletResponse response = ServletActionContext.getResponse();

    // 清缓存

    response.setDateHeader("Expires", 0);

    response.setHeader("Pragma", "no-cache");

    response.setHeader("Cache-Control", "no-cache");

    response.setCharacterEncoding("utf-8");

    response.setContentType("text/html;charset=utf-8");

    PrintWriter out = response.getWriter();

    // 通过ID获取对象

    WorkFlow wf = ws.findById(wfId);

    byte[] defs = wf.getProcessFile();

    System.out.println(defs);

    String wfString = new SAXReader().read(new ByteArrayInputStream(defs))

    .asXML();

    out.print(wfString);

    out.close();

    return null;

    }

    // 查看图片

    public String showImg() {

    HttpServletResponse response = ServletActionContext.getResponse();

    WorkFlow wf = ws.findById(wfId);

    try {

    response.getOutputStream().write(wf.getProcessImg());

    // 清理response

    response.flushBuffer();

    } catch (Exception e) {

    e.printStackTrace();

    }

    return "showImg";

    }

    展开全文
  • 本文实例为大家分享了java根据网络地址保存图片的具体代码,供大家参考,具体内容如下import java.io.BufferedInputStream;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import...
  • 在实际的开发中,我们可能需要将图片、影音等文件直接保存到数据库中,然后通过编程方式将数据读出进行使用。例如将读出的图片数据显示出来,将读出的电影文件播放出来。二进制数据直接保存到文件和从文件中读出非常...
  • /*根据阿里云地址保存到本地*/ public static void download(String urlString, String filename,String savePath) throws Exception { // 构造URL URL url = new URL(urlString); // 打开连接 URLConnection ...
  • packagecom.weixiu.jingwei.utils;importnet.coobird.thumbnailator.Thumbnails;importorg.springframework.web.multipart.MultipartFile;importsun.misc.BASE64Encoder;...importjava.io.FileInput...
  • packagecom.siyang....importjava.io.IOException;importjava.io.InputStream;importjava.io.OutputStream;importjava.io.PrintWriter;importjavax.servlet.http.HttpServletResponse;importorg.slf4j.Logge...
  • 背景:由于近期使用layui写前端,其中用它的控件实现图片上传,拿到的是转成Base64的数据,数据如下:data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAY4AAADvCAYAAADl2zM3AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8...
  • Java保存图片和网页的方式可以说是一样的。需要足以一下我们的文件格式。
  • java 中的图片处理是很让人头疼的一件事情。目前 java api 中的 imageIO 可以将 gif 图片转换成 png 图片, jpg 图片可以正常转换。据说 gif 转 jpg 也是有办法的,但是将 jpg 转成 gif ,我费了很大的工夫才找到一...
  • 本文实例为大家分享了Java从网络读取图片保存至本地的具体代码,供大家参考,具体内容如下package getUrlPic;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileOutputStream;import ...
  • 服务接口 import org.springframework.web.multipart.MultipartFile; /** * 文件服务 * * @author Zhenfeng Li * @version 1.0 ... * 保存/更新图片 * * @param multipartFile 图片 * @param ba
  • java存储图片

    2021-02-12 09:13:53
    importjava.io.File;importjava.io.FileOutputStream;importjava.io.OutputStream;importjava.util.Calendar;importjava.util.Random;importjavax.servlet.http.HttpServletRequest;importorg.springframework.web.c...
  • 图片存储在数据库中是一个客户的要示了,这个方法其实是非常的不好的,因为保存到了数据库中会导致数据库超级大了,但既然做了就给各位分享一下吧.例子代码Class.forName("com.mysql.jdbc.Driver");//System.out....
  • 用途:安卓获取图片(如网络头像,本地剪切后的图片)保存到本地指定路径public class FileUtil {/*** 将Bitmap 图片保存到本地路径,并返回路径* @param c* @param mType 资源类型,参照MultimediaContentType 枚举,...
  • Android 保存图片到设备前言:在许多的应用或游戏中,大多都有保存图片或者截图等等的功能,这篇文档我们的目的是通过 Unity 保存图片,并且调用 Andorid 中的更新相册的原生方法.流程步骤:编写更新相册的 Android 原生...
  • 但是需求是直接保存到本地不通过浏览器下载,这就很尴尬了,咱知道js是基本无法这样操作的,就算强行要操作便是打开activeXObject这种不兼容又有安全问题的东西,所以只好另辟蹊径,用java来搞了。思路:1.原来生成...
  • 在Controller层中设置拦截器,获取URL中的图片(转为InputStream),需要使用二进制字节接收 public void方法名(HttpServletRequest request,HttpServletResponse response) throws IOException { InputStream ...
  • controller 类@PostMapping(value = "uploadImg")@ResponseBodypublic Object uploadImg(MultipartFile[] files){return new DataResult("保存成功!",StatusCode.SUCCESS.getCode(),service.uploadImage(files));}...
  • 记录一下之前项目中用到过的工具类,可以在图片中写入文字并且生成新的图片,记录一下。 文章转载自:https://www.jb51.net/article/162956.htm start ↓ 工具类:PrintImage。 package com.learning.www.utils; ...
  • Java存储图片到Mysql

    2021-03-11 12:09:30
    //3)先在本地存储图片,再读取保存到数据库 if(!file.isDirectory()){ userDetail.setImageName(realPath+"/"+item.getName()); OutputStream os=new FileOutputStream(file); //IOUtils拷贝流 IOUtils.copy(is,os...
  • 关于canvas保存成为图片基本都不能和selenium进行很好的配合,canvas保存图片应用较多的是图片验证或滑动验证的图片保存,下面是利用selenium直接将canvas保存图片的代码 ` // web驱动的存放路径 String ...
  • java文件保存至服务器

    2021-02-27 20:50:19
    import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileOutputStream;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;public class ImageRequest{/***...
  • log.info("图片上传路径:"+filePathName); //判断文件保存是否存在 File file = new File(filePathName); if (file.getParentFile() != null || !file.getParentFile().isDirectory()) { //创建文件 file....
  • 导入两个包commons-codeccommons-io导入okhttpcom.squareup.okhttp3okhttp3.6.0import java.io.*;public class ImageBase64Utils {public static String bytesToBase64(byte[] bytes) {return org.apache.commons....
  • Java codeURL url = new URL(...// 图片不是原来的了。ImageIO.write(ImageIO.read(url),"jpg",new File("image-01.jpg"));//BufferedInputStream input = nu...
  • I want to save the image on disk such as c:/images which is captured by webcam using java ..and again I want to display that image on JForm as a label...is this possible using java and netbeansI'm new...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 262,168
精华内容 104,867
关键字:

java保存图片

java 订阅