Servlet里怎么接收客户端post上来的二进制数据呢?

Active3000 2002-09-13 08:56:48
俺用ServletInputStream不行吗?
有别人做好的现成的类,会用,但不明白其中的道理,所以…能不能给讲解一下?俺是新手,刚登录进来的,。
...全文
378 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
cxj_2000 2002-09-16
  • 打赏
  • 举报
回复
那个是自己定义的一个包
Active3000 2002-09-14
  • 打赏
  • 举报
回复
import com.jetmail.Tool.*;
~~~~~~~~~~~~~~~~~~~~这个东东从哪儿来的?是你自己写的吗?还是使用现成的?俺好好看看,谢谢。
cxj_2000 2002-09-13
  • 打赏
  • 举报
回复
俺给你一个上传的源代码,我写的,版权所有,翻印不究:)

/*
* 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();
}

}

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧