jsp上传文件3

package com;
import java.io.*;
import java.util.*;
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.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import java.sql.*;

public class UpImgServlet extends HttpServlet {

	
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       
    	
    	String savePath = this.getServletConfig().getServletContext().getRealPath("");
    	//String savePath = request.getScheme()+"://" + request.getServerName()+":" + request.getServerPort()
    						//+ request.getContextPath();
        savePath = savePath + "/images/";//保存路径
        File f1 = new File(savePath);
        if(!f1.exists())
        {
        	f1.mkdir();//创建此抽象路径名指定的目录
        }
        
        request.setCharacterEncoding("UTF-8");
        DiskFileItemFactory fac = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(fac);
        upload.setHeaderEncoding("UTF-8");
        List fileList = null;
        try{
        	fileList = upload.parseRequest(request);
        	
        }catch(FileUploadException ex)
        {
        	ex.printStackTrace();
        }
        
        Iterator<FileItem> it = fileList.iterator();
        String name = "";
        String extName = "";
        Map<String , String> map = new HashMap<String , String>();
        while(it.hasNext())
        {
        	FileItem item = it.next();
        	if(!item.isFormField()){
        		name = item.getName();
        		//long size = item.getSize();
        		//String type = item.getContentType();
        		if(name==null||name.trim().equals(""))
        		{
        			continue;
        		}
        		
        		if(name.lastIndexOf(".")>0)
        		{
        			extName = name.substring(name.lastIndexOf("."));
        		}
        		
        		File file = null;
        		do
        		{
        			name = UUID.randomUUID().toString();//产生一个随机的文件名
        			file = new File(savePath + name + extName);
        		}while(file.exists());
        		
        		File saveFile = new File(savePath + name + extName);
        		try{
        			item.write(saveFile);
        		}catch(Exception e)
        		{
        			e.printStackTrace();
        		}
        		
        	}else{//如果是普通的表单元素,如文本框等
        		String value = item.getString("UTF");//原来写的String value = item.getString() 会出现乱码!!!!!!!!!!!
        		String field = item.getFieldName();
        		map.put(field, value);
        	}
        }
        
       // response.getWriter().print(name + extName);
        String pName =  map.get("pname");//不能再用 request.getParameter("pname")!!!!!!!!!!;
System.out.println("name" + pName);
       String pModel = map.get("pmodel");
       String pPrice = map.get("pprice"); 
       String img= name+extName;
       String pInfor = map.get("infor");
       
       Connection conn = DB.getConn();
       PreparedStatement state = DB.getPreState(conn, "insert into produce values(?,?,getDate(),?,?,?) ");
       try {
		state.setString(1, pName);
		state.setString(2, pModel);
		state.setString(3, pPrice);
		state.setString(4, img);
		state.setString(5, pInfor);
		state.executeUpdate();
		
		DB.Close(state);
		DB.Close(conn);
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
       
    }  
    

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doGet(request, response);
    }

    public void init() throws ServletException {
        // Put your code here
    }

    // 以日期 获得一个文件名。(没用到,留着以后用);
    String getFileName() {
        java.util.Calendar cal = Calendar.getInstance();

        int year = cal.get(Calendar.YEAR);
        int mon = cal.get(Calendar.MONTH);
        int day = cal.get(Calendar.DATE);
        int hour = cal.get(Calendar.HOUR);
        int min = cal.get(Calendar.MINUTE);
        int sec = cal.get(Calendar.SECOND);
        int mi = cal.get(Calendar.MILLISECOND);

        System.out.println("mon" + mon);
        System.out.println("day" + day);
        return "" + year + mon + day + hour + min + sec + mi;

    }
    
}

 

你可能感兴趣的:(apache,sql,jsp,servlet)