java中上传图片

1. ActionForm中添加属性,FormFile类型对象。

private FormFile upLoadFile;
public FormFile getUpLoadFile() {
return upLoadFile;
}
public void setUpLoadFile(FormFile upLoadFile) {
this.upLoadFile = upLoadFile;
}
2. Action 中增加修改方法。
public ActionForward save(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
PayDirMenuForm myForm = (PayDirMenuForm) form;
PayDirMenu myObj = new PayDirMenu();
BeanUtils.copyProperties(myObj, myForm);
PayDirMenuManager mgr = (PayDirMenuManager) getBean("PayDirMenuManager");
// 获得文件
FormFile file = myForm.getUpLoadFile();//form中增加的属性
// 获得Tomcate的路径
String objpath = request.getRealPath("plan");
// 获得上传的文件的名字
String fileName = file.getFileName();//获取上传图片的文件名
// 截取字符串,获得后缀名 
    StringsuffixFileName = fileName.substring(fileName.lastIndexOf("."));
//判断上传文件的类型
if(!".txt".equals(suffixFileName)&&!".doc".equals(suffixFileName)&&!".xls".equals(suffixFileName)&&!".jpg".equals(suffixFileName)&&!".gif".equals(suffixFileName)&&!".ppt".equals(suffixFileName))
{
request.getSession().setAttribute("msg1", "<font color='red'>上传的文件格式不正确!</font>");
return null;
}
// 获得内容
byte[] fileData = file.getFileData();
if (myObj.getMenuOid() != null && myObj.getMenuOid().intValue() == 0){  //增加
//写入到流中
FileOutputStream out = new FileOutputStream(new File(objpath + "//"
+ fileName));
String filePath = objpath + "//"+ fileName;
out.write(fileData);
out.close();
out.flush();
//写入数据库
myObj.setMenuImage(filePath);
mgr.saveObj(myObj);
}
else                                        //修改
{
//获得原来上传文件的名字
String menuImage=myObj.getMenuImage();
menuImage=menuImage.substring(0,menuImage.lastIndexOf("."));
//获得新的文件名
menuImage=menuImage+suffixFileName;
FileOutputStream out = new FileOutputStream(new File(objpath + "//"
+ myObj.getMenuImage()));//此处会替换原文件,注意路径
out.write(fileData);
out.close();
out.flush();
//写入数据库
myObj.setMenuImage(menuImage);
mgr.updateObj(myObj);
}
return this.list(mapping, form, request, response);
}
3. jsp页面中
高文娟 (2010-5-5 17:33:48)
<%@ page language="java" pageEncoding="utf-8"%>

<%@include file="/pages/public/taglibs.jsp"%>
<html:html>
<head>
<title>编辑菜单</title>
<link href="pages/admin/images/guanli.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div style="float:right; width:99%;">
<table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#def3fc" style="margin-bottom:5px;">
  <tr>
    <td width="20" height="26">&nbsp;</td>
    <td>当前位置 &gt;&gt; 菜单列表</td>
    <td width="330"> </td>
  </tr>
</table>
 <html:form action="/payDirMenuAction.do?method=save"  method="post" enctype="multipart/form-data">
    <table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#def3fc">
  <tr>
    <td height="8"></td>
    <td></td>
     </tr>
        <tr>
<th>
上传Logo
</th>
<td colspan="3">

<html:file property="upLoadFile"></html:file> <!--此为form中的属性-->
</td>
</tr>
<tr>
<th>菜单名称:</th>
<td align="left" ><input type="text"  name="menuName" id="menuName"  value="${this_one.menuName}" /></td>
   </tr>
<tr >
<th>菜单URL:</th>
<td align="left"><input type="text" name="menuUrl"  id="menuUrl" value="${this_one.menuUrl}" /></td>
</tr>
    <tr >
<th>父菜单ID:</th>
<td align="left"><input type="text" name="parentMenuId" id="parentMenuId" value="${this_one.parentMenuId}" /></td>
 </tr>

<tr >
<th>菜单状态:</th>
<td align="left">
<select name="menuState" >
<option value="1">启用<option>
<option value="2">停用<option>
<select>
</td>

   </tr>
 
<tr >
<td style="width: 130"></td>
<td align="left">
<input type="submit" name="add" value=" 提交 "  /><input type="hidden" name="menuOid" value="${this_one.menuOid}"/>&nbsp;
&nbsp;<button οnclick="javascript:history.go(-1);"> 返 回 </button></td>
</tr>
<tr><td style="height: 30"></td></tr>
</table>
  </html:form>
  </body>
</html:html>


注意:在jsp页面中
1) 用struts的html标签,并且<html:form>中不能有name属性,enctype="multipart/form-data"。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值