1. java servlet 返回靜態資源,靜態資源可以邊下邊播,而通過servlet返回的視頻一定要下載完成後才能播放。
Apache默認是一個靜態伺服器,只能通過它訪問靜態的資源,比如html等。而Servlet的運行必須要支持Servlet標準的Web容器,像Tomcat、Jboss、Weblogic、WebSphere、Jetty等都支持。 有時候也可以將Apache和Tomcat結合起來,各司其職。
2. java如何實現文件上傳和下載的功能
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jspsmart.upload.*;
import net.sf.json.JSONObject;
import action.StudentAction;
public class UploadServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
boolean result=true;
SmartUpload mySmartUpload=new SmartUpload();
mySmartUpload.initialize(this.getServletConfig(), request,response);
mySmartUpload.setTotalMaxFileSize(50*1024*1024);//大小限制
mySmartUpload.setAllowedFilesList("doc,docx");//後綴名限制
try {
mySmartUpload.upload();
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);
myFile.saveAs("/file/"+1+".doc");//保存目錄
} catch (SmartUploadException e) {
e.printStackTrace();result=false;
}
//*****************************//
response.setContentType("text/html;charset=UTF-8");
response.setHeader("Cache-Control","no-cache");
PrintWriter out = response.getWriter();
out.print(result);
out.flush();
out.close();
}
}
//我這是ajax方式的,不想這樣,把//**********************//以下部分修改就行了
//需要SmartUpload組件,去網上下個就行了,也有介紹的