当前位置:多学网学习教育电脑学习编程入门JSP教程jsp技巧大全

jsp技巧大全

[08-23 22:09:45]   来源:http://www.duoxue8.com  JSP教程   阅读:128
jsp技巧大全,标签:JSP编程技巧,jsp开发,jsp实用教程,jsp视频教程,http://www.duoxue8.com

8、setMaxFileSize 

作用:设定每个文件允许上传的最大长度。 

原型:public void setMaxFileSize(long maxFileSize) 

其中,maxFileSize为为每个文件允许上传的最大长度,当文件超出此长度时,将不被上传。 

9、setTotalMaxFileSize 

作用:设定允许上传的文件的总长度,用于限制一次性上传的数据量大小。 

原型:public void setTotalMaxFileSize(long totalMaxFileSize) 

其中,totalMaxFileSize为允许上传的文件的总长度。


jsp 上传图片并生成缩位图或者加水印
有些网站  动网, 上传图片后加给加上自己的字(是在图片上加的)

 请问在JSP里如何实现??
//添加水印,filePath 源图片路径, watermark 水印图片路径
public static boolean createMark(String filePath,String watermark) {
ImageIcon imgIcon=new ImageIcon(filePath);
Image theImg =imgIcon.getImage();
ImageIcon waterIcon=new ImageIcon(watermark);
Image waterImg =waterIcon.getImage();
int width=theImg.getWidth(null);
int height= theImg.getHeight(null);
BufferedImage bimage = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB); 
Graphics2D g=bimage.createGraphics( );
g.setColor(Color.red);
g.setBackground(Color.white);
g.drawImage(theImg, 0, 0, null );
g.drawImage(waterImg, 100, 100, null );
g.drawString("12233",10,10); //添加文字
g.dispose();
try{
FileOutputStream out=new FileOutputStream(filePath);
JPEGImageEncoder encoder =JPEGCodec.createJPEGEncoder(out); 
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage); 
param.setQuality(50f, true); 
encoder.encode(bimage, param); 
out.close();
}catch(Exception e){ return false; }
return true;
}

/////////////////范例////////////////////
package package;

import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;

public class upload
{

private static String newline = "n";
private String uploadDirectory;
private String ContentType;
private String CharacterEncoding;

public upload()
{
uploadDirectory = ".";
ContentType = "";
CharacterEncoding = "";
}

private String getFileName(String s)
{
int i = s.lastIndexOf("\");
if(i < 0 || i >= s.length() - 1)
{
i = s.lastIndexOf("/");
if(i < 0 || i >= s.length() - 1)
return s;
}
return s.substring(i + 1);
}

public void setUploadDirectory(String s)
{
uploadDirectory = s;
}

public void setContentType(String s)
{
ContentType = s;
int i;
if((i = ContentType.indexOf("boundary=")) != -1)
{
ContentType = ContentType.substring(i + 9);
ContentType = "--" + ContentType;
}
}

public void setCharacterEncoding(String s)
{
CharacterEncoding = s;
}

public String uploadFile(HttpServletRequest httpservletrequest)
throws ServletException, IOException
{
String s = null;
setCharacterEncoding(httpservletrequest.getCharacterEncoding());
setContentType(httpservletrequest.getContentType());
s = uploadFile(httpservletrequest.getInputStream());
return s;
}

public String uploadFile(ServletInputStream servletinputstream)
throws ServletException, IOException
{
String s = null;
String s1 = null;
byte abyte0[] = new byte[4096];
byte abyte1[] = new byte[4096];
int ai[] = new int[1];
int ai1[] = new int[1];
String s2;
while((s2 = readLine(abyte0, ai, servletinputstream, CharacterEncoding)) != null) 
{
int i = s2.indexOf("filename=");
if(i >= 0)
{
s2 = s2.substring(i + 10);
if((i = s2.indexOf(""")) > 0)
s2 = s2.substring(0, i);
break;
}
}
s1 = s2;
if(s1 != null && !s1.equals("""))
{
s1 = getFileName(s1);
String s3 = readLine(abyte0, ai, servletinputstream, CharacterEncoding);
if(s3.indexOf("Content-Type") >= 0)
readLine(abyte0, ai, servletinputstream, CharacterEncoding);
File file = new File(uploadDirectory, s1);
FileOutputStream fileoutputstream = new FileOutputStream(file);
while((s3 = readLine(abyte0, ai, servletinputstream, CharacterEncoding)) != null) 
{
if(s3.indexOf(ContentType) == 0 && abyte0[0] == 45)
break;
if(s != null)
{
fileoutputstream.write(abyte1, 0, ai1[0]);
fileoutputstream.flush();
}
s = readLine(abyte1, ai1, servletinputstream, CharacterEncoding);
if(s == null || s.indexOf(ContentType) == 0 && abyte1[0] == 45)
break;
fileoutputstream.write(abyte0, 0, ai[0]);
fileoutputstream.flush();
}
byte byte0;
if(newline.length() == 1)
byte0 = 2;
else
byte0 = 1;
if(s != null && abyte1[0] != 45 && ai1[0] > newline.length() * byte0)
fileoutputstream.write(abyte1, 0, ai1[0] - newline.length() * byte0);
if(s3 != null && abyte0[0] != 45 && ai[0] > newline.length() * byte0)
fileoutputstream.write(abyte0, 0, ai[0] - newline.length() * byte0);
fileoutputstream.close();
}
return s1;
}

private String readLine(byte abyte0[], int ai[], ServletInputStream servletinputstream, String s)
{
ai[0] = servletinputstream.readLine(abyte0, 0, abyte0.length);
if(ai[0] == -1)
return null;
break MISSING_BLOCK_LABEL_27;
Object obj;
obj;
return null;

上一页  [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]  下一页


jsp技巧大全 结束。
Tag:JSP教程JSP编程技巧,jsp开发,jsp实用教程,jsp视频教程电脑学习 - 编程入门 - JSP教程
jsp技巧大全相关文章