Java Web中使用JSPSmartUpload控制元件實現檔案的上傳和下載(解決了中文亂碼問題)(JSP頁面採用GBK編碼)
阿新 • • 發佈:2019-01-09
package edu.uestc.updown;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jspsmart.upload.SmartUpload;
public class ServletUpload extends HttpServlet {
private static final long serialVersionUID = 1L;
private ServletConfig config;
final public void init(ServletConfig config) throws ServletException {
this.config = config;
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<BODY BGCOLOR='white'>");
out.println("<H1>jspSmartUpload : Servlet Sample</H1>");
out.println("<HR>");
// 變數定義
int count = 0;
SmartUpload mySmartUpload = new SmartUpload();
try {
//Smart上傳三句核心程式碼
mySmartUpload.initialize(config, request, response);
mySmartUpload.upload();
count = mySmartUpload.save("/upload");
out.println(count + " file uploaded.");
} catch (Exception e) {
out.println("Unable to upload the file.<br>");
out.println("Error : " + e.toString());
}
out.println("</BODY>");
out.println("</HTML>");
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jspsmart.upload.SmartUpload;
public class ServletUpload extends HttpServlet {
private static final long serialVersionUID = 1L;
private ServletConfig config;
final public void init(ServletConfig config) throws ServletException {
this.config = config;
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<BODY BGCOLOR='white'>");
out.println("<H1>jspSmartUpload : Servlet Sample</H1>");
out.println("<HR>");
// 變數定義
int count = 0;
SmartUpload mySmartUpload = new SmartUpload();
try {
//Smart上傳三句核心程式碼
mySmartUpload.initialize(config, request, response);
mySmartUpload.upload();
count = mySmartUpload.save("/upload");
out.println(count + " file uploaded.");
} catch (Exception e) {
out.println("Unable to upload the file.<br>");
out.println("Error : " + e.toString());
}
out.println("</BODY>");
out.println("</HTML>");
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}