jsp 文件的上传与下载 要求对文件的类型、大小进行选择判断 5

下载的类:packageDown;importjava.io.File;importjava.io.FileInputStream;importjava.io.IOExc... 下载的类:
package Down;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Download extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public Download() {
super();
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String path=new String(request.getParameter("path").getBytes("iso-8859-1"));
File file=new File(path);
InputStream in=new FileInputStream(file);
OutputStream os=response.getOutputStream();
response.addHeader("Content-Disposition","attachment;filename="+new String(file.getName().getBytes("gbk"),"iso-8859-1"));
response.addHeader("Content-Length", String.valueOf(file.length()));
response.setCharacterEncoding("utf-8");
response.setContentType("application/octet-stream");

int data=0;
while((data=in.read())!=-1)
{
os.write(data);
}
os.close();
in.close();

}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub
}
}

servlet类:package Down;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

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

public class Download extends HttpServlet {
private static final long serialVersionUID = 1L;

public Download() {
super();
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String path=new String(request.getParameter("path").getBytes("iso-8859-1"));
File file=new File(path);
InputStream in=new FileInputStream(file);
OutputStream os=response.getOutputStream();
response.addHeader("Content-Disposition","attachment;filename="+new String(file.getName().getBytes("gbk"),"iso-8859-1"));
response.addHeader("Content-Length", String.valueOf(file.length()));
response.setCharacterEncoding("utf-8");
response.setContentType("application/octet-stream");

int data=0;
while((data=in.read())!=-1)
{
os.write(data);
}
os.close();
in.close();

}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
展开
 我来答
903870967
2011-12-10 · 超过41用户采纳过TA的回答
知道小有建树答主
回答量:257
采纳率:0%
帮助的人:116万
展开全部
你下载一个 jspsmartupload 里不是有七个例子,其中一个就是了,你看看
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式