package shop.commodity;//import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
//import java.io.InputStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import shop.model.GoodsBean;
import toolbean.*;
import com.jspsmart.upload.*;
import java.util.Calendar;
public class AddGoodsServlet 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");
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 { //对象创建
GoodsBean goods=new GoodsBean();
DBBean db=new DBBean();
String info="";
String forward="";
String ext="";
String url="upload/";
String picurl="/uploadimage";
FileInputStream imagefile=null;
PreparedStatement pstmt=null;
Connection conn=null;

//处理上传的图片文件
SmartUpload su=new SmartUpload();  //创建新的上传对象
long maxsize=200*1024; //设置每个上传文件的大小为200KB
try{
su.initialize(this.getServletConfig(),request,response);//初始化

su.setMaxFileSize(maxsize); //限制上传文件的大小
su.setDeniedFilesList("exe,bat,jsp,htm,html,,");//设定禁止上传的文件。禁止上传罗列的那些及没有扩展名的文件。
su.setAllowedFilesList("jpg,JPG,gif,GIF"); //设定允许上传的格式
su.upload(); //上传文件
//将上传文件保存到指定目录
su.save(picurl,SmartUpload.SAVE_VIRTUAL);

//获取上传的文件,因为只上传了一个文件,所以可直接获取
com.jspsmart.upload.File myfile=su.getFiles().getFile(0);
if(!myfile.isMissing()){
String filename=myfile.getFilePathName(); //获取完整的带路径的文件名
imagefile=new FileInputStream(filename);
//获取用户提交的其他数据
Request req=su.getRequest();
String cid=req.getParameter("cid");
String cname=req.getParameter("cname");
String number=req.getParameter("cnumber");
String price=req.getParameter("cprice");
String cintro=req.getParameter("cintro");
//转换数量和价格的格式
int cnumber=Integer.parseInt(number);
float cprice=Float.parseFloat(price);

//将图片存到upload文件夹中,并重命名
ext=myfile.getFileExt();
Calendar cal=Calendar.getInstance();
String newfilename=String.valueOf(cal.getTimeInMillis());
//String saveurl=request.getRealPath("/")+url;
//saveurl+=newfilename+"."+ext;

//设置存到数据库中的路径
String cimage=url+newfilename+"."+ext;



// java.io.File iofile = new java.io.File(filename);  
// java.io.FileInputStream fis = new java.io.FileInputStream(iofile);   
//写入到GoodsBean中
// goods.setCid(cid);
// goods.setCname(cname);
// goods.setCnumber(cnumber);
// goods.setCprice(cprice);
// goods.setCintro(cintro);
// goods.setCimage(cimage);

int c=0;

String sql="insert into goods(cid,cname,cnumber,cprice,cintro,cimage,cpic) values (?,?,?,?,?,?,?)";

conn=db.createConn();
conn.setAutoCommit(true);
pstmt=conn.prepareStatement(sql);
pstmt.setString(1,cid);
pstmt.setString(2,cname);
pstmt.setInt(3,cnumber);
pstmt.setFloat(4,cprice);
pstmt.setString(5,cintro);
pstmt.setString(6,cimage);
//pstmt.setBinaryStream(parameterIndex, x, length)
//pstmt.setBinaryStream(6,fis,419);
pstmt.setBinaryStream(7,imagefile,imagefile.available());
//c=pstmt.execute();
c=pstmt.executeUpdate();
if(c==1){
//myfile.saveAs(saveurl,SmartUpload.SAVE_PHYSICAL); //su.SAVE_PHYSICAL
info="添加成功!";
//conn.commit();
forward="c_manager.jsp";
}else{
info="添加失败!";
forward="c_add.jsp";
}

}else{
info="请选择要上传的文件!";
forward="c_add.jsp";
}

}catch(Exception e){
e.printStackTrace();
info="添加异常!";
forward="c_add.jsp";
}finally{
try {pstmt.close();}catch(Exception e){}
try {imagefile.close();}catch(Exception e){}
try {conn.close();}catch(Exception e){}
}
request.setAttribute("info",info);
RequestDispatcher rd=request.getRequestDispatcher(forward);
rd.forward(request,response);

}}

解决方案 »

  1.   

    我也出现了相同的问题,还不抛出异常
    System.out.println("----------------------moreinfo11-------------");
    su.initialize(this.getServletConfig(),request,response); 
    try{System.out.println("----------------------moreinfo22-------------");
    su.upload();
    System.out.println("----------------------moreinfo33-------------");
    int count = su.save("/images");
    if(count==0){out.print("上传照片出现异常,请重新上传!1");
    System.out.println("----------------------moreinfo4444-------------");
    } }catch(Exception e){out.print("上传照片出现异常,请重新上传!3");System.out.println(e);}结果系统输出:
    ----------------------moreinfo11-------------
    ----------------------moreinfo22-------------
    ----------------------moreinfo33-------------
    ----------------------moreinfo4444-------------
    客户端输出:
    ????????,?????!1
      

  2.   

    我的好了,哥们。你看看你的form声明 ENCTYPE='multipart/form-data'了没?