liferay中应用jspsmartupload+servlet实现上传

package com.sict.seller.servlet;

import java.io.IOException;
import java.text.DateFormat;
import java.util.Date;
import java.util.TimeZone;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Session;

import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;
import com.sict.seller.data.SPros;
import com.sict.seller.help.HibernateUtil;

public class Upload extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
ServletConfig config=null;

public void init(ServletConfig config) throws ServletException{
this.config=config;
super.init(config);
}

public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
response.setContentType("text/html;charset=gb2312");
SmartUpload   su   =   new   SmartUpload();    
  //   上传初始化     
su.initialize(config, request, response);
  //   设定上传限制    
  //   1.限制每个上传文件的最大长度。    
  //   su.setmaxfilesize(10000);    
  //   2.限制总上传数据的长度。    
  //   su.settotalmaxfilesize(20000);    
  //   3.设定允许上传的文件,仅允许doc,txt文件。
su.setAllowedFilesList("jpg,jpeg,gif");
//System.out.println( su.getRequest().getParameter("price")+"************");
  //   su.setallowedfileslist("doc,txt");    
  //   4.设定禁止上传的文件,禁止上传带有exe,bat,    
  //jsp,htm,html扩展名的文件和没有扩展名的文件。
try{
su.setDeniedFilesList("exe,bat,jps,htm,html,");
}catch(Exception e){
e.printStackTrace();
System.out.println(e);
}
  //   su.setdeniedfileslist("exe,bat,jsp,htm,html,,");    
  //   上传文件
String name="";
  try{
  su.upload();    
  //   将上传文件全部保存到指定目录    
  int   count   =   su.save("/upload");
  if(count==0){
  name="nopic.gif";
  }else{
  //System.out.println(count+"个文件上传成功!");
  for   (int   i=0;i<su.getFiles().getCount();i++)    
  {    
  com.jspsmart.upload.File   file   =   su.getFiles().getFile(i);     
  if   (file.isMissing())   continue;
  else{
  name=name+file.getFileName();
  }
  }
  }
}catch(Exception e){
e.printStackTrace();
System.out.print(e);
}
addPro(su.getRequest(),name);
response.sendRedirect("/web/11508/1");
}

public void addPro(Request request,String fileName){
String transType=request.getParameter("transType");
String categories=request.getParameter("categories");
String type=request.getParameter("type");
String title=request.getParameter("title");
String description=request.getParameter("description");
String number=request.getParameter("number");
String price=request.getParameter("price");
String provinces=request.getParameter("provinces");
String city=request.getParameter("city");
String freight=request.getParameter("freight");
String invoices=request.getParameter("invoices");
String repair=request.getParameter("repair");
String status="1";//1代表存入仓库
Date time=new Date();
DateFormat gmt08Formatter=DateFormat.getDateTimeInstance();
TimeZone timezone=TimeZone.getTimeZone("GMT+08:00");
gmt08Formatter.setTimeZone(timezone);
// 获得格式化后的东八区时间
String gmt08DateTime=gmt08Formatter.format(time);
Session session = null;

try {
session = HibernateUtil.openSession();
session.beginTransaction();
SPros s=new SPros();
s.setCategories(categories);
s.setCity(city);
s.setDescription(description);
s.setFreight(freight);
s.setInvoices(invoices);
s.setNumber(number);
s.setPicName(fileName);
s.setPrice(price);
s.setType(type);
s.setTransType(transType);
s.setTitle(title);
s.setStatus(status);
s.setRepair(repair);
s.setProvinces(provinces);
s.setTime(gmt08DateTime);
s.setSalingNumber(0+"");
s.setSaledNumber(0+"");
session.save(s);
session.flush();
session.getTransaction().commit();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
HibernateUtil.closeSession(session);
}
}

}

此处注意的问题:
获取页面参数的时候是从SmartUpload   su   =   new   SmartUpload();
Request request=su.getRequest();
String name=request.getParameter("name");

你可能感兴趣的:(html,Hibernate,jsp,Web,servlet)