俺给你一个上传的源代码,我写的,版权所有,翻印不究:)/*
* UpLoadServlet.java
*/
package com.jetmail.Tool;import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import com.jetmail.Tool.*;/*
* 文件上传下载
* @author cxj
* @version 1.0.0
* @date 2002/08/23
*/public class UpLoadServlet extends HttpServlet {
private final int BUFFERSIZE = 1024 * 8;
private final String splitLine = File.separator;
private String tempAttachmentFileName;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//得到附件的路径
HttpSession session = request.getSession(false);
com.jetmail.User.Session mailSession = (com.jetmail.User.Session)session.getAttribute("mailSession");
com.jetmail.Tool.Config mailConfig = com.jetmail.Tool.Config.getInstance();
//计算能够上传附件的大小
int maxLen = mailConfig.attachMentSize * 1024 * 1024;
String attachmentPath = mailConfig.tempFolder + splitLine;
attachmentPath = attachmentPath + mailSession.getDomain() + splitLine;
attachmentPath = attachmentPath + mailSession.getUserName() + splitLine;
attachmentPath = attachmentPath + mailConfig.attachmentFolder + splitLine;
tempAttachmentFileName = mailSession.getDomain() + '-' + mailSession.getUserName();
//得到附件
ServletOutputStream out = response.getOutputStream();
int contentLen = request.getContentLength();
if(contentLen < maxLen) {
String contentType = request.getContentType();
String boundary = getBoundary(contentType);
ServletInputStream in = request.getInputStream();
FileOutputStream fou = null;
byte[] b = new byte[BUFFERSIZE];
int result;
try {
result = in.readLine(b,0,b.length); //读取boundary
result = in.readLine(b,0,b.length); //读取Content-Disposition
String upLoadFileName = getUpLoadFileName(new String(b,0,result));
fou = new FileOutputStream(attachmentPath + tempAttachmentFileName);
result = in.readLine(b,0,b.length); //读取Content-Type;
result = in.readLine(b,0,b.length); //读取空行;
int totalRead = 0;
result = in.readLine(b,0,b.length);
while((new String(b,0,result)).trim().indexOf(boundary) == -1) {
totalRead += result;
fou.write(b,0,result);
result = in.readLine(b,0,b.length);
}
out.println(totalRead);
fou.close();
in.close();
//处理文件
dealFile(upLoadFileName,totalRead,attachmentPath);
} catch(Exception ex) {
System.out.print(ex.toString());
}
}
response.sendRedirect("/jsp/upload.jsp");
}
public void destroy(){}
/*
* 得到filename
*/
private String getUpLoadFileName(String line) {
int split = line.indexOf("filename=");
String tempFileName = delQuote(line.substring(split + 9,line.length()).trim());
if(tempFileName.indexOf("\\") != -1) {
tempFileName = tempFileName.substring(tempFileName.lastIndexOf("\\") + 1,tempFileName.length());
}
return tempFileName;
}
/*
* 得到分隔符
*/
private String getBoundary(String line) {
return line.substring(line.indexOf("boundary=") + 9,line.length()).trim();
}
/*
* 去除""
*/
private String delQuote(String line) {
if(line.indexOf("\"") != -1) {
line = line.substring(1,(line.length() - 1));
}
return line;
}
/*
* 处理文件,把最后的两个字节删除
*/
private void dealFile(String fileName, int totalRead, String attachmentPath) throws IOException {
File file = new File(attachmentPath + tempAttachmentFileName);
byte[] b = new byte[totalRead - 2];
RandomAccessFile rafRead = new RandomAccessFile(file,"r");
rafRead.readFully(b,0,totalRead - 2);
rafRead.close();
file.delete();
RandomAccessFile rafWrite = new RandomAccessFile(attachmentPath + fileName,"rw");
rafWrite.write(b);
rafWrite.close();
}}
* UpLoadServlet.java
*/
package com.jetmail.Tool;import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import com.jetmail.Tool.*;/*
* 文件上传下载
* @author cxj
* @version 1.0.0
* @date 2002/08/23
*/public class UpLoadServlet extends HttpServlet {
private final int BUFFERSIZE = 1024 * 8;
private final String splitLine = File.separator;
private String tempAttachmentFileName;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//得到附件的路径
HttpSession session = request.getSession(false);
com.jetmail.User.Session mailSession = (com.jetmail.User.Session)session.getAttribute("mailSession");
com.jetmail.Tool.Config mailConfig = com.jetmail.Tool.Config.getInstance();
//计算能够上传附件的大小
int maxLen = mailConfig.attachMentSize * 1024 * 1024;
String attachmentPath = mailConfig.tempFolder + splitLine;
attachmentPath = attachmentPath + mailSession.getDomain() + splitLine;
attachmentPath = attachmentPath + mailSession.getUserName() + splitLine;
attachmentPath = attachmentPath + mailConfig.attachmentFolder + splitLine;
tempAttachmentFileName = mailSession.getDomain() + '-' + mailSession.getUserName();
//得到附件
ServletOutputStream out = response.getOutputStream();
int contentLen = request.getContentLength();
if(contentLen < maxLen) {
String contentType = request.getContentType();
String boundary = getBoundary(contentType);
ServletInputStream in = request.getInputStream();
FileOutputStream fou = null;
byte[] b = new byte[BUFFERSIZE];
int result;
try {
result = in.readLine(b,0,b.length); //读取boundary
result = in.readLine(b,0,b.length); //读取Content-Disposition
String upLoadFileName = getUpLoadFileName(new String(b,0,result));
fou = new FileOutputStream(attachmentPath + tempAttachmentFileName);
result = in.readLine(b,0,b.length); //读取Content-Type;
result = in.readLine(b,0,b.length); //读取空行;
int totalRead = 0;
result = in.readLine(b,0,b.length);
while((new String(b,0,result)).trim().indexOf(boundary) == -1) {
totalRead += result;
fou.write(b,0,result);
result = in.readLine(b,0,b.length);
}
out.println(totalRead);
fou.close();
in.close();
//处理文件
dealFile(upLoadFileName,totalRead,attachmentPath);
} catch(Exception ex) {
System.out.print(ex.toString());
}
}
response.sendRedirect("/jsp/upload.jsp");
}
public void destroy(){}
/*
* 得到filename
*/
private String getUpLoadFileName(String line) {
int split = line.indexOf("filename=");
String tempFileName = delQuote(line.substring(split + 9,line.length()).trim());
if(tempFileName.indexOf("\\") != -1) {
tempFileName = tempFileName.substring(tempFileName.lastIndexOf("\\") + 1,tempFileName.length());
}
return tempFileName;
}
/*
* 得到分隔符
*/
private String getBoundary(String line) {
return line.substring(line.indexOf("boundary=") + 9,line.length()).trim();
}
/*
* 去除""
*/
private String delQuote(String line) {
if(line.indexOf("\"") != -1) {
line = line.substring(1,(line.length() - 1));
}
return line;
}
/*
* 处理文件,把最后的两个字节删除
*/
private void dealFile(String fileName, int totalRead, String attachmentPath) throws IOException {
File file = new File(attachmentPath + tempAttachmentFileName);
byte[] b = new byte[totalRead - 2];
RandomAccessFile rafRead = new RandomAccessFile(file,"r");
rafRead.readFully(b,0,totalRead - 2);
rafRead.close();
file.delete();
RandomAccessFile rafWrite = new RandomAccessFile(attachmentPath + fileName,"rw");
rafWrite.write(b);
rafWrite.close();
}}
解决方案 »
- 如何控制浏览器的后退事件
- 很是不解……ActionForm取值问题……
- EL表达式${} 里面怎么判断字符串相等?
- 如何解决跨浏览器问题
- checkbox显示问题?
- 类似csdn搜索功能问题.
- 请问怎样用java接收邮件正文和附件,然后把它显示在网页上???
- 关于采用asp服务代理方式,对不同公司适用统一入口的问题请教!!
- 如何将jsp页面 以word 文档的形式保存到本地
- 在线高分求助: JAVA Applet 如何实现"保存文件对话框"???
- jbuilder+internetbean_express开发jsp程序的dataset的float类型的显示问题
- 奇怪问题,盼高手相助!高分相送!
~~~~~~~~~~~~~~~~~~~~这个东东从哪儿来的?是你自己写的吗?还是使用现成的?俺好好看看,谢谢。