图片上传(ssh)

项目中需添加照片:该方式采用的是存放相对路径
页面:
<form action="addTechnicalsupport.action" id="form" method="post" enctype="multipart/form-data">
<!--enctype="multipart/form-data" 必须-->
<td align="right" bgcolor="#ddeefc" class="STYLE1" >照片:</td>
<td align="center" class="STYLE1">
<input type="file" name="myPhoto" id="myPhoto" style="width:95%" />
</td>
</form>
对应的action
public class TechnicalsupportAction extends CommonAction {
private File myPhoto; //上传的照片
private String myPhotoFileName;
private Technicalsupport technicalsupport; //对象
//上传照片的全名,myPhotoFileName=myPhoto+FileName 自动会得到照片的名称

public String addObject(){
String end = myPhotoFileName.substring(myPhotoFileName.indexOf(".")); //得到图片后缀名
String newFileName = sdf.format(new Date()) + end ; //给图片取一个新名称

if(end.equals(".bmp") || end.equals(".jpg") || end.equals(".gif")){
String photeAbsolutePath = ServletActionContext.getServletContext().getRealPath("/")+"dataDirectory/temp/"+newFileName; //图片的相对路径(图片上传到服务器后存放的位置)
File tempFile = new File(photeAbsolutePath);
FileUtil.copyFile(myPhoto, tempFile); //拷贝文件

String photoRelativePath = request.getContextPath()+"/dataDirectory/temp/"+newFileName; //绝对路径 存放在数据库中

technicalsupport.setPhoto(photoRelativePath); //对象设置路径
//保存对象
}


}

public String getMyPhotoFileName() {
return myPhotoFileName;
}
public void setMyPhotoFileName(String myPhotoFileName) {
this.myPhotoFileName = myPhotoFileName;
}

public File getMyPhoto() {
return myPhoto;
}

public void setMyPhoto(File myPhoto) {
this.myPhoto = myPhoto;
}
public Technicalsupport getTechnicalsupport() {
return technicalsupport;
}

public void setTechnicalsupport(Technicalsupport technicalsupport) {
this.technicalsupport = technicalsupport;
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值