jsp技巧大全
[08-23 22:09:45] 来源:http://www.duoxue8.com JSP教程 阅读:128次prestmt.executeUpdate();
DbaObj.Conn.commit();
out.println(("上载成功!!!").toString());
}
else
{ out.println(("上载失败!!!").toString()); }
}//与前面的if对应
%>
再说一下下载,下载分两种情况1。从数据库直接下载2。从服务器上下载
先说从数据库直接下载的情形:就是把输入流从数据库里读出来,然后转存为文件
<%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="java.sql.*"%>
<%@ page import="java.io.*" %>
<%@ page import="DBstep.iDBManager2000.*"%>
<%
int bytesum=0;
int byteread=0;
//打开数据库
ResultSet result=null;
String Sql=null;
PreparedStatement prestmt=null;
DBstep.iDBManager2000 DbaObj=new DBstep.iDBManager2000();
DbaObj.OpenConnection();
//取得数据库中的数据
Sql="select * from t_local_zhongzhuan ";
result=DbaObj.ExecuteQuery(Sql);
result.next();
//将数据库中的数据读到流中
InputStream inStream=result.getBinaryStream("content");
FileOutputStream fs=new FileOutputStream( "c:/dffdsafd.doc");
byte[] buffer =new byte[1444];
int length;
while ((byteread=inStream.read(buffer))!=-1)
{
out.println("<DT><B>"+byteread+"</B></DT>");
bytesum+=byteread;
System.out.println(bytesum);
fs.write(buffer,0,byteread);
}
%>
再说从服务器上下载的情形:
<%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="java.io.*" %>
<%
String fileName = "zsc104.swf".toString();
f//读到流中
InputStream inStream=new FileInputStream("c:/zsc104.swf");
//设置输出的格式
response.reset();
response.setContentType("bin");
response.addHeader("Content-Disposition","attachment; filename="" + fileName + """);
//循环取出流中的数据
byte[] b = new byte[100];
int len;
while((len=inStream.read(b)) >0)
response.getOutputStream().write(b,0,len);
inStream.close();
%>
好了,到这里只要不是太大的文件的上传下载的操作都可以完成了。
缩略图实现,将图片(jpg,gif,bmp等等)真实的变成想要的大小
import java.io.*;
import java.util.*;
import com.sun.image.codec.jpeg.*;
import java.awt.image.*;
import java.awt.*;
import java.net.*;
import java.applet.*;
import java.sql.*;
//缩略图类,
//本java类能将jpg图片文件,进行等比或非等比的大小转换。
//具体使用方法
//s_pic(大图片路径,生成小图片路径,大图片文件名,生成小图片文名,生成小图片宽度,生成小图片高度,是否等比缩放(默认为true))
public class Small_pic{
String InputDir; //输入图路径
String OutputDir; //输出图路径
String InputFileName; //输入图文件名
String OutputFileName; //输出图文件名
int OutputWidth=80; //默认输出图片宽
int OutputHeight=80; //默认输出图片高
int rate=0;
boolean proportion=true; //是否等比缩放标记(默认为等比缩放)
public Small_pic(){
//初始化变量
InputDir="";
OutputDir="";
InputFileName="";
OutputFileName="";
OutputWidth=80;
OutputHeight=80;
rate=0;
}
public void setInputDir(String InputDir){
this.InputDir=InputDir;
}
public void setOutputDir(String OutputDir){
this.OutputDir=OutputDir;
}
public void setInputFileName(String InputFileName){
this.InputFileName=InputFileName;
}
public void setOutputFileName(String OutputFileName){
this.OutputFileName=OutputFileName;
}
public void setOutputWidth(int OutputWidth){
this.OutputWidth=OutputWidth;
}
public void setOutputHeight(int OutputHeight){
this.OutputHeight=OutputHeight;
}
public void setW_H(int width,int height){
this.OutputWidth=width;
this.OutputHeight=height;
}
public String s_pic(){
BufferedImage image;
String NewFileName;
//建立输出文件对象
File file = new File(OutputDir+OutputFileName);
FileOutputStream tempout =null;
try{
tempout= new FileOutputStream(file);
}catch(Exception ex){
System.out.println(ex.toString());
}
Image img=null;
Toolkit tk=Toolkit.getDefaultToolkit();
Applet app=new Applet();
MediaTracker mt = new MediaTracker(app);
try {
img=tk.getImage(InputDir+InputFileName);
mt.addImage(img, 0);
mt.waitForID(0);
}catch(Exception e) {
e.printStackTrace();
}
if(img.getWidth(null)==-1){
System.out.println(" can't read,retry!"+"<BR>");
return "no";
}else{
int new_w;
int new_h;
if (this.proportion==true) //判断是否是等比缩放.
{
//为等比缩放计算输出的图片宽度及高度
double rate1=((double)img.getWidth(null))/(double)OutputWidth+0.1;
double rate2=((double)img.getHeight(null))/(double)OutputHeight+0.1;
double rate=rate1>rate2?rate1:rate2;
new_w=(int)(((double)img.getWidth(null))/rate);
new_h=(int)(((double)img.getHeight(null))/rate);
}
else{
new_w=OutputWidth; //输出的图片宽度
new_h=OutputHeight; //输出的图片高度
}
BufferedImage buffImg = new BufferedImage(new_w,new_h,BufferedImage.TYPE_INT_RGB);
Graphics g = buffImg.createGraphics();
g.setColor(Color.white);
g.fillRect(0,0,new_w,new_h);
g.drawImage(img,0,0,new_w,new_h,null);
g.dispose();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(tempout);
try{
encoder.encode(buffImg);
tempout.close();
}catch(IOException ex){
System.out.println(ex.toString());
}
}
return "ok";
}
public String s_pic(String InputDir,String OutputDir,String InputFileName,String OutputFileName){
//输入图路径
this.InputDir=InputDir;
//输出图路径
this.OutputDir=OutputDir;
//输入图文件名
this.InputFileName=InputFileName;
//输出图文件名
this.OutputFileName=OutputFileName;
return s_pic();
}
public String s_pic(String InputDir,String OutputDir,String InputFileName,String OutputFileName,int width,int height,boolean gp){
//输入图路径
this.InputDir=InputDir;
//输出图路径
上一页 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] 下一页
jsp技巧大全 结束。