📄 filedownservlet.java~2~
字号:
package com.j2ee.servlet.first;
import com.j2ee.servlet.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class FiledownServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
private File file1;
private RandomAccessFile file2;
// private FileInputStream is;
public FiledownServlet() {
}
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doService(request,response);
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
//所有来自request请求方法归类到下面方法中
public void doService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
ServletOutputStream outStream = response.getOutputStream();
String filename=request.getParameter("name");
String rightf=filename.substring(filename.length()-4);
String ContentType=new String();
//FileInputStream is;
ServletContext app=this.getServletContext();
int flsize=0;
if(rightf.equals("")){
}else if(rightf.equals( ".asf")){
ContentType = "video/x-ms-asf";
}else if(rightf.equals( ".avi")){
ContentType = "video/avi";
}else if(rightf.equals( ".doc")){
ContentType = "application/msword";
}else if(rightf.equals( ".zip")){
ContentType = "application/zip";
}else if(rightf.equals( ".xls")){
ContentType = "application/vnd.ms-excel";
}else if(rightf.equals( ".gif")){
ContentType = "image/gif";
}else if(rightf.equals( ".jpg")||rightf.equals("jpeg")){
ContentType = "image/jpeg";
}else if(rightf.equals( ".wav")){
ContentType = "audio/wav";
}else if(rightf.equals( ".mp3")){
ContentType = "audio/mpeg3";
}else if(rightf.equals( ".mpg")||rightf.equals("mpeg")){
ContentType = "video/mpeg";
}else if(rightf.equals( ".rtf")){
ContentType = "application/rtf";
}else if(rightf.equals( ".htm")||rightf.equals( "html")){
ContentType = "text/html";
}else if(rightf.equals( ".txt")){
ContentType = "text/plain";
}else {
ContentType = "application/octet-stream";
}
response.setContentType(ContentType);
filename="../j2eemanage/upload/"+filename;
filename=app.getRealPath(filename);
file1=new File(filename);
file2=new RandomAccessFile(file1,"r");
flsize=(int)file2.length();
byte []bb=new byte[flsize];
file2.read(bb);
outStream.write(bb);
outStream.close();
}
//Clean up resources
public void destroy() {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -