java 杨鹏 12:12:06
package com.yunhe.auction.web;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class AuctionServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public AuctionServlet() {
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 {
this.doPost(request, response);
}
/**
* 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 {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String opr=request.getParameter("opr");
if("add".equals(opr)){
String webpath=request.getSession().getServletContext().getRealPath("upload");//获取上传到服务器的路径
File savepath=new File(webpath);
if(!savepath.exists()){
savepath.mkdirs();
}
out.println("保存文件路径:"+webpath+"</br>"+"</br>");
FileItemFactory factory=new DiskFileItemFactory();//创建FileItemFactory对象
ServletFileUpload fileUpload=new ServletFileUpload(factory);//创建ServletFileUpload对象
//fileUpload.setSizeMax(1024*30);//设置单个文件的最大限制
List<FileItem> list=null;
try {
list = fileUpload.parseRequest(request);
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//解析form表单中所有文件
for(FileItem item:list){
if(item.isFormField()){
if(item.getFieldName().equals("auctionname")){
String auctionname=item.getString("utf-8");
//out.println(item.getString("utf-8")+" 正在上传文件!"+"</br>"+"</br>");
}
if(item.getFieldName().equals("auctionstartprice")){
String auctionstartprice=item.getString("utf-8");
}
if(item.getFieldName().equals("auctionupset")){
String auctionupset=item.getString("utf-8");
}
if(item.getFieldName().equals("auctionstarttime")){
String auctionstarttime=item.getString("utf-8");
}
if(item.getFieldName().equals("auctionendtime")){
String auctionendtime=item.getString("utf-8");
}
if(item.getFieldName().equals("auctiondesc")){
String auctiondesc=item.getString("utf-8");
}
}else{
String filename=item.getName();//源文件 完整路径
//out.println(filename+"</br>"+"</br>");
//List<String> fileType=Arrays.asList("gif","bmp","jpg","txt");
String ext=filename.substring(filename.lastIndexOf(".")+1);
//if(fileType.contains(ext)){
if(filename!=null){
File sourcefile=new File(filename);//保存文件
String name=sourcefile.getName();//得到文件逻辑名
//out.println(name);
File file=new File(savepath,name);
try {
item.write(file);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.println("上传成功!");
}else{out.println("上传失败!");};
//}else{out.println("上传文件失败,类型只能是:gif、bmp、jpg、txt");}
}
}
}
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}