当前位置:首页 » 编程语言 » java图片导出

java图片导出

发布时间: 2024-04-09 05:25:26

① 请问下java中导出图片怎么做

package com.xolt;
import java.io.FileOutputStream;
import java.io.File;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import java.awt.image.BufferedImage;
import javax.imageio.*;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;;

public class TestPOI {

public static void main(String[] args) {
FileOutputStream fileOut = null;
BufferedImage bufferImg =null;
BufferedImage bufferImg1 = null;
try{

//先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
ByteArrayOutputStream byteArrayOut1 = new ByteArrayOutputStream();
bufferImg = ImageIO.read(new File("C:/Documents and Settings/dingqi/Desktop/clip_image002.jpg"));
bufferImg1 = ImageIO.read(new File("C:/Documents and Settings/dingqi/Desktop/clip_image002.jpg"));
ImageIO.write(bufferImg,"jpg",byteArrayOut);
ImageIO.write(bufferImg1,"jpg",byteArrayOut1);

//创建一个工作薄
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("poi picT");
//HSSFRow row = sheet1.createRow(2);
HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor(0,0,512,255,(short) 1,1,(short)10,20);
HSSFClientAnchor anchor1 = new HSSFClientAnchor(0,0,512,255,(short) 2,30,(short)10,60);
anchor1.setAnchorType(2);
//插入图片
patriarch.createPicture(anchor , wb.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));
patriarch.createPicture(anchor1 , wb.addPicture(byteArrayOut1.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));

fileOut = new FileOutputStream("c:/workbook.xls");
//写入excel文件
wb.write(fileOut);
fileOut.close();

}catch(IOException io){
io.printStackTrace();
System.out.println("io erorr : "+ io.getMessage());
} finally
{
if (fileOut != null)
{

try {
fileOut.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}

poi中图片到到excel的方法 你需要准备poi包 试试看看

② java怎么在控制台输出一张jpg的图片

输出图片的base64编码

//imgFile是图片的路径
publicstaticvoidgetImageStr(StringimgFile){
InputStreaminputStream=null;
byte[]data=null;
try{
inputStream=newFileInputStream(imgFile);
data=newbyte[inputStream.available()];
inputStream.read(data);
inputStream.close();
}catch(IOExceptione){
e.printStackTrace();
}//加密
BASE64Encoderencoder=newBASE64Encoder();
System.out.println(encoder.encode(data));
}

③ java从服务器下载图片怎么讲图片保存到本地的sdcard上

ublic HttpServletResponse download(String path, HttpServletResponse response) {
try {
// path是指欲下载的文件的路径。
File file = new File(path);
// 取得文件名。
String filename = file.getName();
// 取得文件的后缀名。
String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();

// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(path));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
response.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
}
return response;
}

④ 用java语言我怎么把图片存放到数据库然后在取出来啊

如果要存数据库的话,数据库存图片字段用blob形式的(照片:zp为例)。
而且不能直接存,在存之前zp字段先插入一个empty.BLOB(),
然后select ZP from 表 for update。再用输入流的形式写进去。
// 先检索出来字段,必须使用oracle的类:oracle.sql.BLOB
oracle.sql.BLOB blob = null;
if (rs.next())
{
blob = (oracle.sql.BLOB) rs.getBlob("ZP");
// 到数据库的输出流
OutputStream outStream = blob.getBinaryOutputStream();
// 将输入流写到输出流
byte[] b = new byte[blob.getBufferSize()];
int len = 0;
while ((len = is.read(b)) != -1)
{
outStream.write(b, 0, len);
// blob.putBytes(1,b);
}
is.close();
outStream.flush();
outStream.close();
}
取照片的话,取出来转化成流的形式直接创建jpg文件就行了。
Blob b = rs.getBlob("ZP");
File f = null;
if (b != null) {
is = b.getBinaryStream();
f = new File( "c:\\zp.jpg");
if (!f.exists()) {
f.createNewFile();
}
os = new FileOutputStream(f);
int len;
byte buf[] = new byte[2048];
while ((len = is.read(buf)) != -1) {
os.write(buf, 0, len);
}
is.close();
os.flush();
os.close();
}
强烈建议只存取照片路径,这样方便。

⑤ Java中怎么抓取网页中的图片

通过httpclient来爬取网站内容,分析当前内容页中的图片‘规则’
抓取一般都是模拟浏览器访问目标网页,通过返回的页面html代码进行分析自己需要的数据
查找规则,例如你爬取的网页 ,看到当前页面显示的图片格式如下<img src="http://www..com/img/20101025_user.png">
通过解析爬取的网页源代码(html)进行字符串的操作即可,现在有相应的第三方jar包可以帮你更快的完成这部分工作,例如htmlpaser,获取到对应的地址,然后进行保存或下载。
你可以搜索,java爬虫(httpclient)和htmlpaser做更多的了解。

⑥ 请教java导出多张图片到Excel问题!

package tei;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class TestExcelImage {
static List<BufferedImage> images = new ArrayList<>();
static {
try {
images.add(ImageIO.read(new File("C:/t/1.jpg")));
images.add(ImageIO.read(new File("C:/t/2.jpg")));
images.add(ImageIO.read(new File("C:/t/3.jpg")));
images.add(ImageIO.read(new File("C:/t/4.jpg")));
images.add(ImageIO.read(new File("C:/t/5.jpg")));
images.add(ImageIO.read(new File("C:/t/6.jpg")));
images.add(ImageIO.read(new File("C:/t/7.jpg")));
images.add(ImageIO.read(new File("C:/t/8.jpg")));
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
FileOutputStream fileOut = null;
try {
// 创建一个工作薄
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
// HSSFRow row = sheet1.createRow(2);
HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
short i = 0;
for (BufferedImage image : images) {
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", byteArrayOut);
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) 1, 1 + i, (short) 2, 2 + i);
anchor.setAnchorType(0);
// 插入图片
patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
i++;
}
HSSFRow row = sheet1.createRow(10);
short s = 10;
HSSFCell cell = row.createCell(s);
HSSFCellStyle style = wb.createCellStyle();
HSSFFont font = wb.createFont();
font.setStrikeout(true);
style.setFont(font);
cell.setCellStyle(style);
cell.setCellValue("aaaaa");
fileOut = new FileOutputStream("c:/workbook.xls");
// 写入excel文件
wb.write(fileOut);
fileOut.close();
} catch (IOException io) {
io.printStackTrace();
System.out.println("io erorr : " + io.getMessage());
} finally {
if (fileOut != null) {
try {
fileOut.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}

简单的写了下。 我机器上可以运行

⑦ Java编写时,怎么输出一张图片呀

(1)首先用ImageIO类读取这张图片
(2)如果要对这张图片修改,通过图片获取Graphics对象,再调用Graphics的方法来绘制、修改。

(3)再调用ImageIO的方法将图片输出到特定IO流即可。

具体代码实例可参考李刚的疯狂Java讲义的11.8节。

⑧ java如何将图片保存在数据库中(java保存图片到本地)

一般都是这样的,就是在你服务器有一个专门放置图片的文件夹,然后数据库保存的是你服务器图片的路径。需要用的时候就去数据库里面取路径。得到路径以后你蔽哪想怎么处理图片是你的事情了。

至于如何去数据库取路径这个就是简单的db操作。

加载驱动类:

Class.forName(DBDriver);

获取连接:

Connectionconn=.(url,username,password);

创建操作对象:

stmt=con.(sql);

执行操作:

ResultSetrs=stmt.();

遍历卖毕结果:

Listlist=newArrayList();

while(rs.next()){

//具体操作,通常用rs.getString(name)取值

Imageimg=newImage();//图片类对应你数据库中图片表格

img.setSrc(rs.getString("src"));//假设你数据库中image表中图片地址字段是src

list.add(img);

}

记得关闭资源:

rs.close();

stmt.close();

con.close();

看你的意思是已经中并芹取出来了不知道怎么显示:

你取出来之后可以把图片放在一个list里面然后去页面上遍历这个list

大致应该是这样

热点内容
arm编译添加驱动 发布:2024-05-21 02:02:28 浏览:475
安卓设置页面是怎么 发布:2024-05-21 01:32:51 浏览:520
学生成绩管理系统数据库设计 发布:2024-05-21 01:14:41 浏览:42
我的世界什么指令直接出现服务器 发布:2024-05-21 01:10:00 浏览:397
星等算法 发布:2024-05-21 00:53:06 浏览:509
李兴华的java视频 发布:2024-05-21 00:49:55 浏览:605
数据库4种索引类型 发布:2024-05-21 00:47:29 浏览:241
给服务器添加另一个ip 发布:2024-05-21 00:40:37 浏览:821
搭建ftp服务器出现微软蓝屏 发布:2024-05-21 00:35:18 浏览:369
ftp怎么加照片 发布:2024-05-21 00:14:37 浏览:623