實現(xiàn)思路:無論是何種類型,都是轉(zhuǎn)換為流的形式進行的文件傳輸和存儲。
十年的小店網(wǎng)站建設經(jīng)驗,針對設計、前端、開發(fā)、售后、文案、推廣等六對一服務,響應快,48小時及時工作處理。成都全網(wǎng)營銷推廣的優(yōu)勢是能夠根據(jù)用戶設備顯示端的尺寸不同,自動調(diào)整小店建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設計,從而大程度地提升瀏覽體驗。成都創(chuàng)新互聯(lián)從事“小店網(wǎng)站設計”,“小店網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。
可以通過BufferedReader 流的形式進行流緩存,之后通過readLine方法獲取到緩存的內(nèi)容。
BufferedReader bre = null;
OutputStreamWriter pw = null;//定義一個流
try {
String file = "D:/test/test.GIF";
bre = new BufferedReader(new FileReader(file));//此時獲取到的bre就是整個文件的緩存流
pw = new OutputStreamWriter(new FileOutputStream(“D:/New.GIF”),"GBK");//確認流的輸出文件和編碼格式,此過程創(chuàng)建了“test.GIF”實例
while ((str = bre.readLine())!= null) // 判斷最后一行不存在,為空結(jié)束循環(huán)
{
pw.write(str);//將要寫入文件的內(nèi)容,可以多次write
};
bre.close();
pw.close();//關(guān)閉流
備注:文件流用完之后必須及時通過close方法關(guān)閉,否則會一直處于打開狀態(tài),直至程序停止,增加系統(tǒng)負擔。
這是代碼,你可以自己調(diào)試一下。
數(shù)據(jù)結(jié)構(gòu)如下:
CREATE TABLE dtree (
id int,
pid int,
name varchar(200),
url varchar(200),
title varchar(200),
target varchar(200),
icon varchar(200),
iconopen varchar(200),
opened bit);
為了實現(xiàn)獲取數(shù)據(jù)庫變量功能,需要建立一個DTree類,并編譯生成CLASS文件,放入\WEB-INF\classes文件夾下。
DTree類代碼如下:
package work3;
public class DTree {
private int id;
private int pid;
private String name;
private String url;
private String title;
private String target;
private String icon;
private String iconOpen;
private int opened;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getTarget() {
return target;
}
public void setTarget(String target) {
this.target = target;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public String getIconOpen() {
return iconOpen;
}
public void setIconOpen(String iconOpen) {
this.iconOpen = iconOpen;
}
public int getOpened() {
return opened;
}
public void setOpened(int opened) {
this.opened = opened;
}
}
work3.jsp代碼如下:
%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%
%@ page import="java.sql.*"%
jsp:useBean id='settree' scope="application" class="work3.DTree" /
!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
""
html
head
meta http-equiv="Content-Type" content="text/html; charset=GB18030"
link rel="StyleSheet" href="dtree.css" type="text/css" /
script type="text/javascript" src="dtree.js"/script
titledTree in MySQL/title
/head
body
h2
Example
/h2
div class="dtree"
p
a href="javascript: d.openAll();"open all/a |
a href="javascript: d.closeAll();"close all/a
/p
script type="text/javascript"
!--
d = new dTree('d');
%
//驅(qū)動程序名
String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
//數(shù)據(jù)庫用戶名
String userName = "sa";
//密碼
String userPwd = "1";
//數(shù)據(jù)庫名
String dbName = "master";
//表名
String tableName = "dtree";
//連接字符串
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName="+dbName;
//加載驅(qū)動
Class.forName(driverName).newInstance();
//連接數(shù)據(jù)庫
java.sql.Connection conn = DriverManager.getConnection(url,userName,userPwd);
//得到Statement實例
java.sql.Statement statement = conn.createStatement();
//查詢數(shù)據(jù)
String sql = "select * from " + tableName;
//返回結(jié)果
java.sql.ResultSet rs = statement.executeQuery(sql);
//獲取變量
while (rs.next()) {
settree.setId(rs.getInt(1));
settree.setPid(rs.getInt(2));
settree.setName(rs.getString(3));
settree.setUrl(rs.getString(4));
settree.setTitle(rs.getString(5));
settree.setTarget(rs.getString(6));
settree.setIcon(rs.getString(7));
settree.setIconOpen(rs.getString(8));
settree.setOpened(rs.getInt(9));
if(settree.getPid()==0)
settree.setOpened(1);
%
d.add(%=settree.getId()%,%=settree.getPid()%,'%=settree.getName()%','%=settree.getUrl()%','%=settree.getTitle()%','%=settree.getTarget()%','','',%=settree.getOpened()%);
%
}
%
document.write(d);
//--
/script
/div
/body
/html
import?java.awt.Canvas;
import?java.awt.Color;
import?java.awt.Dimension;
import?java.awt.EventQueue;
import?java.awt.Frame;
import?java.awt.Graphics;
import?java.awt.Graphics2D;
import?java.awt.Image;
import?java.awt.RenderingHints;
import?java.awt.event.KeyEvent;
import?java.awt.event.KeyListener;
import?java.awt.event.WindowAdapter;
import?java.awt.event.WindowEvent;
import?java.awt.image.BufferedImage;
import?java.io.File;
import?java.io.IOException;
import?javax.imageio.ImageIO;
public?class?TestImage?extends?Frame
{
private?static?final?long?serialVersionUID?=?1L;
private?static?boolean?PRESSED?=?false;
private?static?int?pointX?=?0;
private?static?int?pointy?=?200;
private?static?int?RIGHT_GO?=?0;
private?static?int?LEFT_GO?=?0;
private?static?int?DIR?=?0;
private?static?int?ANGLE?=?0;
private?static?int?W?=?50;
private?static?int?H?=?60;
private?_Canvas?canvas?=?null;
public?TestImage?()
{
add?(canvas?=?new?_Canvas?());
setIgnoreRepaint?(true);
requestFocus?();
}
public?class?_Canvas?extends?Canvas?implements?Runnable
{
private?static?final?long?serialVersionUID?=?1L;
private?BufferedImage?bi?=?null;
private?Image?bufferedImage?=?null;
private?Thread?thread?=?null;
private?long?sleepTime?=?10;
public?_Canvas?()
{
try
{
bi?=?ImageIO.read?(new?File?("go.png"));
}
catch?(IOException?e)
{}
setBackground?(Color.BLACK);
requestFocus?();
addKeyListener?(new?KeyListener?()
{
@Override
public?void?keyTyped?(?KeyEvent?e?)
{}
@Override
public?void?keyReleased?(?KeyEvent?e?)
{
RIGHT_GO?=?0;
PRESSED?=?false;
}
@Override
public?void?keyPressed?(?KeyEvent?e?)
{
//?38?40?37?39上下左右
DIR?=?e.getKeyCode?();
PRESSED?=?true;
}
});
}
@Override
public?void?paint?(?Graphics?g?)
{
Graphics2D?g2d?=?(Graphics2D)?g;
g2d.setRenderingHint?(RenderingHints.KEY_INTERPOLATION,?RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.drawImage?(rotateImage?(bi.getSubimage?(RIGHT_GO,?LEFT_GO,?W,?H),?ANGLE,?true),?pointX,?pointy,?W,?H,
this);
g2d.dispose?();
}
@Override
public?void?update?(?Graphics?g?)
{
if?(null?==?bufferedImage)
{
bufferedImage?=?createImage?(getWidth?(),?getHeight?());
}
Graphics?bufferedG?=?bufferedImage.getGraphics?();
bufferedG.clearRect?(0,?0,?getWidth?(),?getHeight?());
paint?(bufferedG);
bufferedG.dispose?();
g.drawImage?(bufferedImage,?0,?0,?this);
g.dispose?();
}
public?void?start?()
{
thread?=?new?Thread?(this);
thread.setName?("TestImage");
thread.setPriority?(Thread.MIN_PRIORITY);
thread.start?();
}
public?synchronized?void?stop?()
{
thread?=?null;
notify?();
}
@Override
public?void?run?()
{
Thread?me?=?Thread.currentThread?();
while?(thread?==?me??!isShowing?()?||?getSize?().width?==?0)
{
try
{
Thread.sleep?(555);
}
catch?(InterruptedException?e)
{
return;
}
}
while?(thread?==?me??isShowing?())
{
if?(PRESSED)
{
try
{
if?(DIR?==?39)
{
RIGHT_GO?=?RIGHT_GO?+?50;
LEFT_GO?=?0;
pointX?=?pointX?+?1;
if?(pointX??420)
{
ANGLE?=?90;
pointX--;
pointy--;
W?=?60;
H?=?50;
}
if?(RIGHT_GO??50)
{
RIGHT_GO?=?0;
}
}
else?if?(DIR?==?37)
{
pointX?=?pointX?-?1;
RIGHT_GO?=?RIGHT_GO?+?50;
LEFT_GO?=?60;
if?(pointX??0)
{
ANGLE?=?-90;
pointX++;
pointy--;
W?=?60;
H?=?50;
}
if?(RIGHT_GO??50)
{
RIGHT_GO?=?0;
}
}
else?if?(DIR?==?38)
{
W?=?50;
H?=?60;
pointy?=?150;
ANGLE?=?0;
RIGHT_GO?=?100;
}
else?if?(DIR?==?40)
{
W?=?50;
H?=?60;
ANGLE?=?0;
pointy?=?200;
RIGHT_GO?=?0;
}
Thread.sleep?(sleepTime);
repaint?();
}
catch?(InterruptedException?e)
{
break;
}
}
else
{
RIGHT_GO?=?RIGHT_GO?+?50;
LEFT_GO?=?0;
pointX?=?pointX?+?1;
if?(RIGHT_GO??50)
{
RIGHT_GO?=?0;
}
if?(pointX??500)
{
pointX?=?0;
}
try
{
Thread.sleep?(sleepTime);
repaint?();
}
catch?(InterruptedException?e)
{
break;
}
}
}
thread?=?null;
}
}
/**
?*?旋轉(zhuǎn)圖像為指定角度
?*?
?*?@param?degree
?*?@return
?*/
public?static?BufferedImage?rotateImage?(?final?BufferedImage?image,?final?int?angdeg,?final?boolean?d?)
{
int?w?=?image.getWidth?();
int?h?=?image.getHeight?();
int?type?=?image.getColorModel?().getTransparency?();
BufferedImage?img;
Graphics2D?graphics2d;
(?graphics2d?=?(?img?=?new?BufferedImage?(w,?h,?type)?).createGraphics?()?).setRenderingHint?(
RenderingHints.KEY_INTERPOLATION,?RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2d.rotate?(d???-Math.toRadians?(angdeg)?:?Math.toRadians?(angdeg),?w?/?2,?h?/?2);
graphics2d.drawImage?(image,?0,?0,?null);
graphics2d.dispose?();
return?img;
}
public?static?void?main?(?String[]?args?)
{
EventQueue.invokeLater?(new?Runnable?()
{
@Override
public?void?run?()
{
final?TestImage?ti?=?new?TestImage?();
ti.setSize?(new?Dimension?(500,?300));
ti.setLocationRelativeTo?(null);
ti.addWindowListener?(new?WindowAdapter?()
{
@Override
public?void?windowClosing?(?WindowEvent?e?)
{
System.exit?(0);
}
@Override
public?void?windowDeiconified?(?WindowEvent?e?)
{
ti.canvas.start?();
}
@Override
public?void?windowIconified?(?WindowEvent?e?)
{
ti.canvas.stop?();
}
});
ti.setResizable?(false);
ti.canvas.start?();
ti.setVisible?(true);
}
});
}
}