📄 uploadfile.java
字号:
package org.chooseClass.servlet;
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 javax.servlet.jsp.JspFactory;
import javax.servlet.jsp.PageContext;
import org.chooseClass.smartUpLoad.File;
import org.chooseClass.smartUpLoad.SmartUpload;
import org.chooseClass.smartUpLoad.SmartUploadException;
public class UploadFile extends HttpServlet {
/**
* Constructor of the object.
*/
public UploadFile() {
super();
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @param pageContext
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
//创建SmartUpload实例
SmartUpload su = new SmartUpload();
//初始化上下文环境
//su.initialize(pageContext);
//上传文件
try {
su.upload("gb2312");
} catch (SmartUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//取得上传的文件
for (int i=0;i<su.getFiles().getCount();i++)
{
File tFile = su.getFiles().getFile(0);
if(!tFile.isMissing()){
//取得上载文件的名字
String fileName = tFile.getFileName();
//取得不带后缀的文件名
String suffix = fileName.substring(0, fileName.lastIndexOf("."));
//取得后缀名
String ext = tFile.getFileExt();
//取得上传文件的大小
int fileSize = tFile.getSize();
//保存路径
String savePath = getServletContext().getRealPath("/")+"upload\\";
String trace = savePath+fileName;
//将文件保存在服务器端
try {
tFile.saveAs(trace,su.SAVE_PHYSICAL);
} catch (SmartUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.println("上传成功!");
}else{
out.println("上传失败!");
}
}
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @param pageContext
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
//获得页面输出流
JspFactory _jspxFactory = JspFactory.getDefaultFactory();;
PageContext pageContext = null;
//创建SmartUpload实例
SmartUpload su = new SmartUpload();
pageContext = _jspxFactory.getPageContext(this, request,response, null, true, 8192, true);
//if(pageContext!=null){System.out.println("ok");}
//初始化上下文环境
su.initialize(pageContext);
//上传文件
try {
su.upload();
} catch (SmartUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
int count = su.save("upload");
System.out.println(getServletContext().getRealPath("/"));
} catch (SmartUploadException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//取得上传的文件
for (int i=0;i<su.getFiles().getCount();i++)
{
File tFile = su.getFiles().getFile(1);
if(!tFile.isMissing()){
//取得上载文件的名字
String fileName = tFile.getFileName();
//取得不带后缀的文件名
String suffix = fileName.substring(0, fileName.lastIndexOf("."));
//取得后缀名
String ext = tFile.getFileExt();
//取得上传文件的大小
int fileSize = tFile.getSize();
//保存路径
String savePath = getServletContext().getRealPath("/")+"upload\\";
String trace = savePath+fileName;
//将文件保存在服务器端
try {
tFile.saveAs(trace,su.SAVE_PHYSICAL);
} catch (SmartUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.println("上传成功!");
}else{
out.println("上传失败!");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -