我现在想实现一个上传功能 我不想用jspsmartupload组件
我想自己写一个上传类
通过http协议上传
主要实现的功能是能同时上传多个图片
并且能把上传form表单的值一并获取过来 ,然后存入到oracle的blob字段中
[我想通过临时文件上传]
现在遇到了麻烦
我不知道如何处理了
希望有谁做过这个功能的朋友给说一下思路
最好有源码的 我先谢过了
呵呵
public class getImage extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK"; public void init(ServletConfig config) throws ServletException {
super.init(config);
} public void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
Tool tool = new Tool();
ServletInputStream inStream = request.getInputStream(); //获取上传文件流
int streamLenth = request.getContentLength(); //获取上传文件的长度 //将文件存入临时文件中
FileOutputStream tmpfileStream = null;
String tmpfileName = null;
tmpfileName = "c:\\temp\\" + request.getSession().getId();
tmpfileStream = new FileOutputStream(tmpfileName);
int bytesRead = 0, totalBytes = 0;
byte[] cnt;
while (totalBytes < streamLenth) {
cnt = new byte[256];
bytesRead = inStream.read(cnt, 0, 256);
totalBytes += bytesRead;
tmpfileStream.write(cnt);
}
tmpfileStream.close();
inStream.close();
//现已将Http协议上传的内容全部写入了临时文件。以下的代码便可在服务器上对上传的内容进行解释
//以随机读写方式打开http临时流文件。
// RandomAccessFile(String name, String mode) - 类 java.io.RandomAccessFile 的构造方法
// 创建从中读取和向其中写入(可选)的随机存取文件流,该文件具有指定名称。
// name - 取决于系统的文件名 mode - 此存取 mode
long cntStartPoint = 0, cntEndPoint = 0;
RandomAccessFile randomFile = null;
randomFile = new RandomAccessFile(tmpfileName, "rw");
cntStartPoint = randomFile.getFilePointer();
String sgmtDeli = randomFile.readLine();
//out.println(sgmtDeli);
String line=null;
boolean eofblock = false;
while (!eofblock) {
line = randomFile.readLine();
//out.println(line+"<br />");
if(line.equals(sgmtDeli)){
out.println(line+"<br />");
}
if (line.contains(sgmtDeli+"--"))
eofblock = true;
}
out.close();
}
// public String cntEndPoint(String line,String sgmtDeli){
// String bonuy=
// }
}
这是我的主要代码
做到这里我就不知道下一步该怎么做了
谢谢了
我想自己写一个上传类
通过http协议上传
主要实现的功能是能同时上传多个图片
并且能把上传form表单的值一并获取过来 ,然后存入到oracle的blob字段中
[我想通过临时文件上传]
现在遇到了麻烦
我不知道如何处理了
希望有谁做过这个功能的朋友给说一下思路
最好有源码的 我先谢过了
呵呵
public class getImage extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK"; public void init(ServletConfig config) throws ServletException {
super.init(config);
} public void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
Tool tool = new Tool();
ServletInputStream inStream = request.getInputStream(); //获取上传文件流
int streamLenth = request.getContentLength(); //获取上传文件的长度 //将文件存入临时文件中
FileOutputStream tmpfileStream = null;
String tmpfileName = null;
tmpfileName = "c:\\temp\\" + request.getSession().getId();
tmpfileStream = new FileOutputStream(tmpfileName);
int bytesRead = 0, totalBytes = 0;
byte[] cnt;
while (totalBytes < streamLenth) {
cnt = new byte[256];
bytesRead = inStream.read(cnt, 0, 256);
totalBytes += bytesRead;
tmpfileStream.write(cnt);
}
tmpfileStream.close();
inStream.close();
//现已将Http协议上传的内容全部写入了临时文件。以下的代码便可在服务器上对上传的内容进行解释
//以随机读写方式打开http临时流文件。
// RandomAccessFile(String name, String mode) - 类 java.io.RandomAccessFile 的构造方法
// 创建从中读取和向其中写入(可选)的随机存取文件流,该文件具有指定名称。
// name - 取决于系统的文件名 mode - 此存取 mode
long cntStartPoint = 0, cntEndPoint = 0;
RandomAccessFile randomFile = null;
randomFile = new RandomAccessFile(tmpfileName, "rw");
cntStartPoint = randomFile.getFilePointer();
String sgmtDeli = randomFile.readLine();
//out.println(sgmtDeli);
String line=null;
boolean eofblock = false;
while (!eofblock) {
line = randomFile.readLine();
//out.println(line+"<br />");
if(line.equals(sgmtDeli)){
out.println(line+"<br />");
}
if (line.contains(sgmtDeli+"--"))
eofblock = true;
}
out.close();
}
// public String cntEndPoint(String line,String sgmtDeli){
// String bonuy=
// }
}
这是我的主要代码
做到这里我就不知道下一步该怎么做了
谢谢了
解决方案 »
- 推荐个美国JAVA虚拟主机,JDK1.5+TOMCAT5 + MYSQL5+ SSH的。
- 关于webService调用外部类遇到的问题,请大神们帮帮忙,谢谢!
- 如何将保质期预警消息插入到数据库中
- 明明已经释放连接池了,还是提示错误。请大家帮忙看看阿
- 请教一个FireFox插件Firebug的问题
- 域名和ip地址登录问题 急急!!
- 100求解决方案,用过hibernate+struts+spring的请进
- 写了一个日语日记系统,怎么设置字符串编码格式能够正确保存到数据库正确显示?
- JSP社区程序BBS-CS V2.1发布!
- 大家说说Jsp的不足吧!!!
- java接口开发
- 怎样正确读出csv文件的+号?
当然如果你是出于学习,到是值得鼓励!请问这个servlet写到这出现了什么问题没有?
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.PageContext;public class yuanyifileup
{
private ServletRequest request;
private ServletResponse response;
private ServletConfig config;
ServletInputStream DATA;
int FormSize;
File f1;
FileOutputStream os;
DataInputStream is;
String filename;
byte[] b;
byte t;
boolean flag=false;
public yuanyifileup()
{ }
public void initialize(ServletConfig config,HttpServletRequest request,HttpServletResponse response) throws IOException
{
this.request=request;
this.response=response;
this.config=config;
DATA = request.getInputStream();
FormSize=request.getContentLength();
}
public void initialize(PageContext pageContext) throws IOException
{
request=pageContext.getRequest();
response=pageContext.getResponse();
config=pageContext.getServletConfig();
DATA = request.getInputStream();
FormSize=request.getContentLength();
}
public boolean setFilename(String s)
{
try
{
File f1=new File(s);
os=new FileOutputStream(f1);
}
catch(IOException e)
{return(false);}
return(true);
}
public void getByte()
{
int i=0;
try
{
is=new DataInputStream(DATA);
b=new byte[FormSize];while (true)
{
try
{
t=is.readByte();
b[i]=t;
i++;
}
catch(EOFException e)
{ break;}
}
is.close();}
catch(IOException e)
{}
}public boolean save()
{
int i=0,start1=0,start2=0;
String temp="";
if (!flag)
{
getByte();
flag=true;
}
try
{
temp=new String(b,"ISO8859_1");
}
catch(UnsupportedEncodingException e)
{return(false);}start1=temp.indexOf("image/");
temp=temp.substring(start1);start1=temp.indexOf(" ");temp=temp.substring(start1+4);
start2=temp.indexOf("; ");
if (start2!=-1)
{
temp=temp.substring(0,start2);
}
try
{
byte[] img=temp.getBytes("ISO8859_1");
for (i=0;i<img.length;i++)
{ os.write(img[i]); }
os.close();
}
catch(IOException e)
{return(false);}return(true);}
可以参考一下