社区
Web 开发
帖子详情
十万火急!!!!各位大虾们,请问如何在JSP中上载图片?????
douthy
2002-03-08 09:43:47
如何将非服务器上的图片上传到服务器,小弟我急用!!!
...全文
45
5
打赏
收藏
十万火急!!!!各位大虾们,请问如何在JSP中上载图片?????
如何将非服务器上的图片上传到服务器,小弟我急用!!!
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
5 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
douthy
2002-03-09
打赏
举报
回复
谢谢你啊!
不过我现在还有个问题,按你的方法可以上传了,可是为什么在我这个程序中又不可以呢?
<%@ page contentType="text/html;charset=GB2312" import="java.util.*,up.upload" %>
<%
addnews=request.getParameter("addnews");
if(addnews!=null){
type=(request.getParameter("type"));
title=(request.getParameter("title")).replace('%','~');
author=(request.getParameter("author"));
contents=(request.getParameter("content")).replace('%','~');
image_1=(request.getParameter("image_1"));
java.util.StringTokenizer st=new java.util.StringTokenizer(contents,"\n");
String s="";
while(st.hasMoreTokens()){
s+=st.nextToken()+"<br>";
}
String image="/image/";
upload up=new upload();
up.init(application);
up.setUploadDirectory(image);
String filename=up.uploadFile(request);
int result=sys.setInsert(request,tableName,type,title,author,filename,image_2,image_3,s);
%>
为什么这样取得filename为空呢?
douthy
2002-03-08
打赏
举报
回复
谢谢你啊!!!!
怎么给分啊
yanchang
2002-03-08
打赏
举报
回复
/******************************* 例 子 *************************************
file.html
<form enctype="multipart/form-data" method=post action =up.jsp>
请上传您的文件:<br>
<input type=file name = myfile><br>
<input type=submit value="提 交">
</form>
up.jsp
<%@ page import = "pub.upload" %>
<%
String Dir = "/upload/";
upload up = new upload();
up.init(application);
up.setUploadDirectory(Dir);
String filename = up.uploadFile(request);
if (filename != null)
{
out.print("文件"+filename+"已经成功上传到"+Dir);
}
%>
********************************** 结 束 ***********************************/
分啊!~~~~~~~~
yanchang
2002-03-08
打赏
举报
回复
用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 < 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)
{
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<filename.length();i++)
{
if((int)filename.charAt(i)==32)
{
filename=filename.replace(filename.charAt(i),'_');
}
}
String sContentType = readLine(Linebyte, ai, servletinputstream, CharacterEncoding);
if(sContentType.indexOf("Content-Type") >= 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)
yanchang
2002-03-08
打赏
举报
回复
点管理啊!
别望了给分
:)
哈哈
非常感谢您留心我这份系统诊断报告,小菜鸟
十万火急
等待您的帮助!
经典 非常感谢您留心我这份系统诊断报告,小菜鸟
十万火急
等待您的帮助! 该诊断报告由360安全卫士提供 http://www.360safe.com 诊断时间: 2008-12-10 13:44:18
ImageBox网页
图片
批量下载工具 8.0.5
在网页
图片
下载领域,ImageBox表现出色,它能轻松应对复杂的网页结构,无论是简单的
图片
链接,还是嵌入在JavaScript或者CSS
中
的
图片
,都能够被精准识别并下载。这对于需要大量
图片
素材的设计师、摄影师、网站开发者...
小学成语大赛模板PPT学习教案.pptx
这些成语的引入,让孩子们在游戏
中
学习,提高他们的记忆力和语言应用能力。 此外,教案还包含了一些数学元素,比如成语与数字的结合,如"( 一丝不苟 ) + ( 九霄云外 ) = (
十万火急
)"这样的算式,既锻炼了孩子们的...
小学成语大赛PPT课件.pptx
这个课件包含88页内容,覆盖了多个不同类型的成语,旨在激发孩子们的兴趣,让他们在游戏
中
学习。 课件的规则简单明了,适合小组竞赛,每个学生轮流答题,每答对一题则为所在小组加分。试题分为三个类别:看图猜成语...
小学语文近义词形容情况危急的成语
在小学语文教学
中
,成语的教授不仅是一种语言积累的过程,更是对学生情感表达和思想理解能力的一种培养。近义词和成语的学习能够帮助学生用更加精确和生动的词汇来描述复杂的情感与情境,尤其是那些形容情况危急的...
Web 开发
81,115
社区成员
341,737
社区内容
发帖
与我相关
我的任务
Web 开发
Java Web 开发
复制链接
扫一扫
分享
社区描述
Java Web 开发
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章