📄 uploadpictureservlet.java
字号:
import java.io.IOException;
import java.io.PrintWriter;
import java.io.*;
import com.jspsmart.upload.*;
import java.util.*;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.*;
import userpicture.pictureBean;
public class UploadPictureServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public UploadPictureServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* 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
* @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();
}
/**
* 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
* @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();*/
//String file=request.getParameter("file");
HttpSession session = request.getSession(true);
String myid=(String)(session.getAttribute("id"));
//System.out.println(myid);
//System.out.println(file);
String mydir=(String)session.getAttribute("dir");
String mytype=(String)session.getAttribute("type");
//String mydir = request.getParameter("dir");
//String mytype = request.getParameter("type");
System.out.println("insert into :"+mydir+" "+mytype);
//System.out.println(mytype);
mydir=myid;
String path = request.getRealPath("");
path ="C:\\test\\"+myid;
java.io.File d = new java.io.File(path);// 建立代表Sub目录的File对象,并得到它的一个引用
if (d.exists()) {// 检查Sub目录是否存在
//d.delete();
out.println("myid目录存在,已删除");
} else {
d.mkdir();// 建立myid目录
out.println("myid目录不存在,已建立");
}
mydir=(String)session.getAttribute("dir");
path ="C:\\test\\"+myid+"\\"+mydir; // 将要建立的目录路径
d = new java.io.File(path);// 建立代表Sub目录的File对象,并得到它的一个引用
if (d.exists()) {// 检查Sub目录是否存在
//d.delete();
out.println("myid目录存在,已删除");
} else {
d.mkdir();// 建立myid目录
out.println("myid目录不存在,已建立");
}
SmartUpload su = new SmartUpload();
try {
JspFactory _jspxFactory = null;
PageContext pageContext = null;
_jspxFactory = JspFactory.getDefaultFactory();
pageContext = _jspxFactory.getPageContext(this, request, response,
"", true, 8192, true);
su.initialize(pageContext);
// 设定上传限制
// 1.限制每个上传文件的最大长度。
su.setMaxFileSize(400000);
// 2.限制总上传数据的长度。
su.setTotalMaxFileSize(1500000);
// 3.设定允许上传的文件(通过扩展名限制),仅允许doc,txt文件。
su.setAllowedFilesList("jpg,gif,JPG");
// 4.设定禁止上传的文件(通过扩展名限制),禁止上传带有exe,bat,jsp,htm,html扩展名的文件和没有扩展名的文件。
su.setDeniedFilesList("exe,bat,jsp,htm,html,,");
su.initialize(pageContext);
su.upload();
//su.save("/");
// su.save("G:/test/zhong/");
com.jspsmart.upload.File f = su.getFiles().getFile(0);
//f.saveAs("/"+f.getFieldName());
//f.SAVEAS_PHYSICAL();
f.saveAs("C:\\test\\"+myid+"\\"+mydir+"\\"+f.getFileName());
// f.saveAs("c:\\temp\\" + f.getFileName(),
// su.SAVE_PHYSICAL);
out.println("<SCRIPT language=javascript> alert('upload success!');</SCRIPT>");
pageContext.forward("../Upload.jsp");
mydir=(String)session.getAttribute("dir");
pictureBean myp = new pictureBean();
myp.setId(myid);
myp.setFilename(f.getFileName());
myp.setDir(mydir);
myp.setType(mytype);
myp.upload();
//System.out.println(f.getFieldName());
} catch (Exception e) {
}
}
/**
* The doPut method of the servlet. <br>
*
* This method is called when a HTTP put request is received.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPut(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Put your code here
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -