文件上传

文件上传用cos.jar还是非常方便的,代码如下,可以分为2部分,第一部分就是负责命名的内部类,如以下代码:
static class MyFile implements FileRenamePolicy{
static int flagId=0; 
static String curStr="";
static String newName="";
static int seq=1;
public File rename(File f) {
newName=addInNameExt(f.getName(),getDateStr());
return new File(f.getParentFile(),newName);
}
private String getDateStr(){
String pattern="yyyyMMddHHmmss";
SimpleDateFormat sdf=new SimpleDateFormat(pattern);
String dateStr=sdf.format(new java.util.Date());
return dateStr;
}
private String addInNameExt(String fullName,String add){
String name="";
String ext="";
char point='.';
int index=fullName.lastIndexOf(point);
if(index!=-1)
{
name=fullName.substring(0,index);
ext=fullName.substring(index+1);
}else{
name=fullName;
}
return add+flagId+point+ext;
}
}



这是一个内部的静态类,主要负责传参数时的内部命名,第二部分就是文件上传部分:如google订餐系统中的增加菜的功能:
public ActionForward insertfood(HttpServletRequest request,
HttpServletResponse response, ActionMapping mapping)
throws Exception {
HttpSession session=request.getSession();
Eatery eatery=(Eatery) session.getAttribute("eatery");
//
ServletContext application=request.getSession().getServletContext();//获取tomcate的绝对路径
String path=application.getRealPath("/");
//
File file=new File(path+"foodimages");
if(!file.exists())
{
boolean createfile=file.mkdir();
}
String saveDir=path+"foodimages";
int maxPostSize=100*1024*1024;
String encoding="GBK";
MyFile myfile=new MyFile();
MultipartRequest multRequest=null;
myfile.flagId=eatery.getId();
try {
multRequest=new MultipartRequest(request, saveDir, maxPostSize, encoding, myfile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String txtname=multRequest.getParameter("txtname");//获得菜名
String txtingredient=multRequest.getParameter("txtingredient");//获得配料
double txtprice=Double.parseDouble(multRequest.getParameter("txtprice"));//价格
float txtagio=Float.parseFloat(multRequest.getParameter("txtagio"));//折扣
String txtcharacteristics=multRequest.getParameter("txtcharacteristics");//特色
String weizhi="/foodimages/"+myfile.newName;//图片文件位置
int stcuisine=Integer.parseInt(multRequest.getParameter("stcuisine"));//获得菜系
int Id=Integer.parseInt(multRequest.getParameter("Id"));//餐馆Id
String txtremark=multRequest.getParameter("txtremark");//备注
EateryService comservice=new EateryService();
int rows=comservice.insertfood(txtname, txtingredient, txtprice, txtagio, txtcharacteristics, weizhi, stcuisine, Id, txtremark);
if(rows>0)
{
return new ActionForward("/eateryjsp/addfoodsucc.jsp");
}else
{
request.setAttribute("mistake","服务器超时,请重新登录!");
return new ActionForward("/eateryLogin.jsp");
}
}


只要路径没问题,都可以。另外,如果做删除菜的时候,应该也把菜对应的图片删除了,不然服务器硬盘里的垃圾会越来越多,如:
public ActionForward deleatfood(HttpServletRequest request,
HttpServletResponse response, ActionMapping mapping)
throws Exception {
HttpSession session=request.getSession();
int foodId=Integer.parseInt(request.getParameter("foodId"));
Foods foods=new Foods();
EateryService comservice=new EateryService();
foods=comservice.getFoodByFoodId(foodId);
ServletContext application=request.getSession().getServletContext();//获取tomcate的绝对路径
String path=application.getRealPath("/");
String picturepath=path+foods.getFoodImg();
File fileimage=new File(picturepath);
if(fileimage.exists())
{
boolean succdelete=fileimage.delete();
}
int rows=comservice.deleatfood(foodId);
if(rows>0)
{
Eatery eaterys=new Eatery();
eaterys=(Eatery) session.getAttribute("eatery");
return new ActionForward("/findfoodsinfo.do?Id="+eaterys.getId()+"");
}else
{
request.setAttribute("mistake","服务器超时,请重新登录!");
return new ActionForward("/eateryLogin.jsp");
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值