跪求!!!一个servlet实现文件上传的例子

tubage408 2007-12-27 05:16:52
我想学学文件上传,现在是用servlet做的,但是解析输入流的时候总是不对,我的页面上除了file外还有几个text,select我同时也想得到他们的值,我不知道有uploadfile包和jspsmartupload包可不可以。于是我直接解析的到的流,但是解析的时候总是出错,所以来这里请高手帮助,希望前辈们给个例子,最后是我用的那种方式,谢谢
...全文
130 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
我是小辉辉 2007-12-29
  • 打赏
  • 举报
回复
对了,我这个是写死向D盘存一个ZIP文件,那么我上传的也是一个ZIP文件,你可以自行修改,
我是小辉辉 2007-12-29
  • 打赏
  • 举报
回复
我自己写了一个,但注意,在有些系统上可能不支持中文路径:
如果你想再传其它参数,应该把参数手动拼到URL后面如:form1.action=form1.action+"?arg1="+form1.name.value;
upload.jsp代码:
<%@ page language="java"
pageEncoding="GBK"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<form name="form1" action="/upload/UploadServlet" method="POST" ENCTYPE="multipart/form-data">
<input type="file" name="file1"/><br>
复核人:<input name="verify" value="12312312312"/><input name="verify1" value="12312312312"/><br>
<input type="submit"value="上传"/>
</form>
</body>
</html>
UploadServlet 代码:
package servlet;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import save.SaveFile;

public class UploadServlet extends HttpServlet {
private static final String LINE_SEPARATOR ="\r\n";

/**
* Constructor of the object.
*/
public UploadServlet() {
super();
}

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

doPost(request,response);
}

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String contentType = request.getContentType();
InputStream in1=request.getInputStream();
int offset = -1;
StringBuffer sb=new StringBuffer();
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("D:\\abc.zip"));
byte[] b=new byte[4096];
while((offset=in1.read(b,0,4096))>-1){
sb.append(new String(b,0,offset,"UTF-8"));
bos.write(b, 0, offset);
}
bos.flush();
bos.close();

}


public void init() throws ServletException {
// Put your code here
}

}
leem06 2007-12-29
  • 打赏
  • 举报
回复
jspsmart网上资料很多,都是下完直接就能用的
胡矣 2007-12-29
  • 打赏
  • 举报
回复
smartupload就行啊
网上很多例子的
swu_zuifeng 2007-12-29
  • 打赏
  • 举报
回复
好像有个文件上传的控件吧,我当初就是用的那个。
xiaxinhuo 2007-12-29
  • 打赏
  • 举报
回复
我每次都是网上找的一找来就可以用,几乎都不需要有什么改的!

62,635

社区成员

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

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