当前位置: 技术问答>java相关
十万火急!!!!各位大虾们,请问如何在JSP中上载图片?????
来源: 互联网 发布时间:2015-04-12
本文导语: 如何将非服务器上的图片上传到服务器,小弟我急用!!! | 用smartupload www.jspsmart.com 给你个例子 /** * 功能:这是一个文件上载类,把一个指定的文件从客户端上传到服务器指定的...
如何将非服务器上的图片上传到服务器,小弟我急用!!!
|
用smartupload
www.jspsmart.com
给你个例子
/** * 功能:这是一个文件上载类,把一个指定的文件从客户端上传到服务器指定的目录
* 参数:String newline 一个新行
* String uploadDirectory 目标文件夹
* String ContentType 测试request的类型
* String CharacterEncoding 指定的编码格式
*
* 方法:void init(ServletContext app) 参数指定为application,使成员变量application
* 为当前页面的application对象
* String getFileName(String s) 返回上传的文件名
* setUploadDirectory(String s) 设置目标文件夹
* setContentType(String s) 设置request类型
* setCharacterEncoding(String s) 设置编码格式
* String uploadFile(HttpServletRequest req) 初始化環境,返回文件名
* String uploadFile(ServletInputStream servletinputstream) 重载上一涵数,写入文件。返回文件名
*
* 编写: 诗剑书生
* 时间: 2000-12-25
*/
//package pub;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class upload
{
private static String newline = "n";
private String uploadDirectory = ".";
private String ContentType = "";
private String CharacterEncoding = "";
private ServletContext application;
public final void init(ServletContext app)
throws ServletException
{
application = app;
}
private String getFileName(String s)
{
int i = s.lastIndexOf("\");
if(i = s.length() - 1)
{
i = s.lastIndexOf("/");
if(i = s.length() - 1) return s;
}
return s.substring(i + 1);
}
public void setUploadDirectory(String s)
{
if (isVirtual(s))
{
uploadDirectory = application.getRealPath(s);
}
else
{
uploadDirectory = s;
}
File testdir = new File(uploadDirectory);
if (!testdir.exists())
{
throw new IllegalArgumentException(String.valueOf((new StringBuffer("Directory '")).append(s).append("' not found (1040).")));
}
}
public void setContentType(String s)
{
ContentType = s;
int j;
if((j = ContentType.indexOf("boundary=")) != -1)
{
ContentType = ContentType.substring(j + 9);
ContentType = "--" + ContentType;
}
}
public void setCharacterEncoding(String s)
{
CharacterEncoding = s;
}
public String uploadFile(HttpServletRequest req)
throws ServletException,IOException
{
setCharacterEncoding(req.getCharacterEncoding());
setContentType(req.getContentType());
String filename = uploadFile(req.getInputStream());
return filename;
}
public String uploadFile(ServletInputStream servletinputstream)
throws ServletException,IOException
{
String s5 = null;
String filename = null;
byte Linebyte[] = new byte[4096];
byte outLinebyte[] = new byte[4096];
int ai[] = new int[1];
int ai1[] = new int[1];
String line;
while((line = readLine(Linebyte, ai, servletinputstream, CharacterEncoding)) != null)
{
int i = line.indexOf("filename=");
if(i >= 0)
{
line = line.substring(i + 10);
if((i = line.indexOf(""")) > 0)
line = line.substring(0, i);
break;
}
}
filename = line;
if(filename != null && !filename.equals("""))
{
filename = getFileName(filename);
for (int i=0;i= 0)
readLine(Linebyte, ai, servletinputstream, CharacterEncoding);
//File(String parent, String child)
//Creates a new File instance from a parent pathname string
//and a child pathname string.
File file = new File(uploadDirectory, filename);
//FileOutputStream(File file)
//Creates a file output stream to write to the file represented
//by the specified File object.
FileOutputStream fileoutputstream = new FileOutputStream(file);
while((sContentType = readLine(Linebyte, ai, servletinputstream, CharacterEncoding)) != null)
{
if(sContentType.indexOf(ContentType) == 0 && Linebyte[0] == 45)
break;
if(s5 != null)
{
//write(byte[] b, int off, int len)
//Writes len bytes from the specified byte array starting
//at offset off to this file output stream.
fileoutputstream.write(outLinebyte, 0, ai1[0]);
fileoutputstream.flush();
}
s5 = readLine(outLinebyte, ai1, servletinputstream, CharacterEncoding);
if(s5 == null || s5.indexOf(ContentType) == 0 && outLinebyte[0] == 45)
break;
fileoutputstream.write(Linebyte, 0, ai[0]);
fileoutputstream.flush();
}
byte byte0;
if(newline.length() == 1)
byte0 = 2;
else
byte0 = 1;
if(s5 != null && outLinebyte[0] != 45 && ai1[0] > newline.length() * byte0)
fileoutputstream.write(outLinebyte, 0, ai1[0] - newline.length() * byte0);
if(sContentType != null && Linebyte[0] != 45 && ai[0] > newline.length() * byte0)
fileoutputstream.write(Linebyte, 0, ai[0] - newline.length() * byte0);
fileoutputstream.close();
}
return filename;
}
private boolean isVirtual(String s)
{
if(application.getRealPath(s) != null)
{
java.io.File file = new java.io.File(application.getRealPath(s));
return file.exists();
}
else
{
return false;
}
}
private String readLine(byte Linebyte[], int ai[], ServletInputStream servletinputstream, String CharacterEncoding)
{
try
{
//readLine(byte[] buffer, int offset, int length)
//Reads a line from the POST data.
ai[0] = servletinputstream.readLine(Linebyte, 0, Linebyte.length);
if(ai[0] == -1)
return null;
}
catch(IOException _ex)
{
return null;
}
try
{
if(CharacterEncoding == null)
{
//用缺省的编码方式把给定的byte数组转换为字符串
//String(byte[] bytes, int offset, int length)
return new String(Linebyte, 0, ai[0]);
}
else
{
//用给定的编码方式把给定的byte数组转换为字符串
//String(byte[] bytes, int offset, int length, String enc)
return new String(Linebyte, 0, ai[0], CharacterEncoding);
}
}
catch(Exception _ex)
www.jspsmart.com
给你个例子
/** * 功能:这是一个文件上载类,把一个指定的文件从客户端上传到服务器指定的目录
* 参数:String newline 一个新行
* String uploadDirectory 目标文件夹
* String ContentType 测试request的类型
* String CharacterEncoding 指定的编码格式
*
* 方法:void init(ServletContext app) 参数指定为application,使成员变量application
* 为当前页面的application对象
* String getFileName(String s) 返回上传的文件名
* setUploadDirectory(String s) 设置目标文件夹
* setContentType(String s) 设置request类型
* setCharacterEncoding(String s) 设置编码格式
* String uploadFile(HttpServletRequest req) 初始化環境,返回文件名
* String uploadFile(ServletInputStream servletinputstream) 重载上一涵数,写入文件。返回文件名
*
* 编写: 诗剑书生
* 时间: 2000-12-25
*/
//package pub;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class upload
{
private static String newline = "n";
private String uploadDirectory = ".";
private String ContentType = "";
private String CharacterEncoding = "";
private ServletContext application;
public final void init(ServletContext app)
throws ServletException
{
application = app;
}
private String getFileName(String s)
{
int i = s.lastIndexOf("\");
if(i = s.length() - 1)
{
i = s.lastIndexOf("/");
if(i = s.length() - 1) return s;
}
return s.substring(i + 1);
}
public void setUploadDirectory(String s)
{
if (isVirtual(s))
{
uploadDirectory = application.getRealPath(s);
}
else
{
uploadDirectory = s;
}
File testdir = new File(uploadDirectory);
if (!testdir.exists())
{
throw new IllegalArgumentException(String.valueOf((new StringBuffer("Directory '")).append(s).append("' not found (1040).")));
}
}
public void setContentType(String s)
{
ContentType = s;
int j;
if((j = ContentType.indexOf("boundary=")) != -1)
{
ContentType = ContentType.substring(j + 9);
ContentType = "--" + ContentType;
}
}
public void setCharacterEncoding(String s)
{
CharacterEncoding = s;
}
public String uploadFile(HttpServletRequest req)
throws ServletException,IOException
{
setCharacterEncoding(req.getCharacterEncoding());
setContentType(req.getContentType());
String filename = uploadFile(req.getInputStream());
return filename;
}
public String uploadFile(ServletInputStream servletinputstream)
throws ServletException,IOException
{
String s5 = null;
String filename = null;
byte Linebyte[] = new byte[4096];
byte outLinebyte[] = new byte[4096];
int ai[] = new int[1];
int ai1[] = new int[1];
String line;
while((line = readLine(Linebyte, ai, servletinputstream, CharacterEncoding)) != null)
{
int i = line.indexOf("filename=");
if(i >= 0)
{
line = line.substring(i + 10);
if((i = line.indexOf(""")) > 0)
line = line.substring(0, i);
break;
}
}
filename = line;
if(filename != null && !filename.equals("""))
{
filename = getFileName(filename);
for (int i=0;i= 0)
readLine(Linebyte, ai, servletinputstream, CharacterEncoding);
//File(String parent, String child)
//Creates a new File instance from a parent pathname string
//and a child pathname string.
File file = new File(uploadDirectory, filename);
//FileOutputStream(File file)
//Creates a file output stream to write to the file represented
//by the specified File object.
FileOutputStream fileoutputstream = new FileOutputStream(file);
while((sContentType = readLine(Linebyte, ai, servletinputstream, CharacterEncoding)) != null)
{
if(sContentType.indexOf(ContentType) == 0 && Linebyte[0] == 45)
break;
if(s5 != null)
{
//write(byte[] b, int off, int len)
//Writes len bytes from the specified byte array starting
//at offset off to this file output stream.
fileoutputstream.write(outLinebyte, 0, ai1[0]);
fileoutputstream.flush();
}
s5 = readLine(outLinebyte, ai1, servletinputstream, CharacterEncoding);
if(s5 == null || s5.indexOf(ContentType) == 0 && outLinebyte[0] == 45)
break;
fileoutputstream.write(Linebyte, 0, ai[0]);
fileoutputstream.flush();
}
byte byte0;
if(newline.length() == 1)
byte0 = 2;
else
byte0 = 1;
if(s5 != null && outLinebyte[0] != 45 && ai1[0] > newline.length() * byte0)
fileoutputstream.write(outLinebyte, 0, ai1[0] - newline.length() * byte0);
if(sContentType != null && Linebyte[0] != 45 && ai[0] > newline.length() * byte0)
fileoutputstream.write(Linebyte, 0, ai[0] - newline.length() * byte0);
fileoutputstream.close();
}
return filename;
}
private boolean isVirtual(String s)
{
if(application.getRealPath(s) != null)
{
java.io.File file = new java.io.File(application.getRealPath(s));
return file.exists();
}
else
{
return false;
}
}
private String readLine(byte Linebyte[], int ai[], ServletInputStream servletinputstream, String CharacterEncoding)
{
try
{
//readLine(byte[] buffer, int offset, int length)
//Reads a line from the POST data.
ai[0] = servletinputstream.readLine(Linebyte, 0, Linebyte.length);
if(ai[0] == -1)
return null;
}
catch(IOException _ex)
{
return null;
}
try
{
if(CharacterEncoding == null)
{
//用缺省的编码方式把给定的byte数组转换为字符串
//String(byte[] bytes, int offset, int length)
return new String(Linebyte, 0, ai[0]);
}
else
{
//用给定的编码方式把给定的byte数组转换为字符串
//String(byte[] bytes, int offset, int length, String enc)
return new String(Linebyte, 0, ai[0], CharacterEncoding);
}
}
catch(Exception _ex)