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 {
}
} 展开
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 {
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |