📄 addphpoto.java
字号:
package com.webalbum2.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.config.ModuleConfig;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.webalbum2.bean.*;
public class addphpoto extends HttpServlet {
/**
* 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");
response.setCharacterEncoding("gbk");
PrintWriter out = response.getWriter();
try {
request.setCharacterEncoding("gbk");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyyMMddHHmmss");
java.util.Date currentTime = new java.util.Date();//得到当前系统时间
String t = formatter.format(currentTime); //将日期时间格式化
ServletConfig servletConfig = getServletConfig();
// 新建一个SmartUpload对象
SmartUpload su = new SmartUpload();
// 上传初始化
su.initialize(servletConfig, request, response);
// 设定上传限制
// 1.限制每个上传文件的最大长度。
// su.setMaxFileSize(10000);
// 2.限制总上传数据的长度。
// su.setTotalMaxFileSize(20000);
// 3.设定允许上传的文件(通过扩展名限制),仅允许doc,txt文件。
// su.setAllowedFilesList("doc,txt");
// 4.设定禁止上传的文件(通过扩展名限制),禁止上传带有exe,bat,jsp,htm,html扩展名的文件和没有扩展名的文件。
// su.setDeniedFilesList("exe,bat,jsp,htm,html,,");
// 上传文件
try {
su.upload();
} catch (SmartUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 将上传文件全部保存到指定目录
String filename="/uploadphoto/"+t+".jpg";
try {
su.getFiles().getFile(0).saveAs(filename);
} catch (SmartUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String photo_name = su.getRequest().getParameter("photo_name");
String photo_des = su.getRequest().getParameter("photo_des");
//String photo_patn = su.getRequest().getParameter("photo_path");
String album =su.getRequest().getParameter("album_id");
int album_id=Integer.parseInt(album);
int photo_isdefault=0;
String photo_path=t+".jpg";
dbcontrol db=new dbcontrol();
try {
db.insertphoto(album_id, photo_des, photo_path,photo_name);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
db.close();
// int count = su.save("/uploadphoto");
out.println("<script> alert('上传成功');location.href=\"main.jsp\"; </script> ");
// 利用Request对象获取参数之值
// out.println("TEST="+su.getRequest().getParameter("TEST")
// +"<BR><BR>");
// 逐一提取上传文件信息,同时可保存文件。
// for (int i=0;i<su.getFiles().getCount();i++)
// {
// com.jspsmart.upload.File file = su.getFiles().getFile(i);
// 若文件不存在则继续
// if (file.isMissing()) continue;
// 显示当前文件信息
// out.println("<TABLE BORDER=1>");
// out.println("<TR><TD>表单项名(FieldName)</TD><TD>"
// + file.getFieldName() + "</TD></TR>");
// out.println("<TR><TD>文件长度(Size)</TD><TD>" +
// file.getSize() + "</TD></TR>");
// out.println("<TR><TD>文件名(FileName)</TD><TD>"
// + file.getFileName() + "</TD></TR>");
// out.println("<TR><TD>文件扩展名(FileExt)</TD><TD>"
// + file.getFileExt() + "</TD></TR>");
// out.println("<TR><TD>文件全名(FilePathName)</TD><TD>"
// + file.getFilePathName() + "</TD></TR>");
// out.println("</TABLE><BR>");
// 将文件另存
// file.saveAs("/upload/" + myFile.getFileName());
// 另存到以WEB应用程序的根目录为文件根目录的目录下
// file.saveAs("/upload/" + myFile.getFileName(), su.SAVE_VIRTUAL);
// 另存到操作系统的根目录为文件根目录的目录下
// file.saveAs("c:\temp\" + myFile.getFileName(), su.SAVE_PHYSICAL);
//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 {
doGet(request,response);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -