當前位置:首頁 » 編程語言 » 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

大致應該是這樣

熱點內容
40款方舟編譯過的軟體 發布:2024-05-10 17:24:45 瀏覽:481
java在人工智慧 發布:2024-05-10 17:23:11 瀏覽:907
minecraft基岩版搭建伺服器 發布:2024-05-10 17:22:30 瀏覽:323
傳播源碼罪 發布:2024-05-10 17:22:07 瀏覽:398
linux負載命令 發布:2024-05-10 17:18:49 瀏覽:609
move拒絕訪問 發布:2024-05-10 17:18:47 瀏覽:491
邁騰怎麼連接安卓carplay 發布:2024-05-10 17:07:44 瀏覽:723
linux伺服器io怎麼查 發布:2024-05-10 17:07:41 瀏覽:609
是會吃編程 發布:2024-05-10 17:06:59 瀏覽:357
怎麼剪輯水印安卓 發布:2024-05-10 17:06:57 瀏覽:539