项目中需添加照片:该方式采用的是存放相对路径
页面:
<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;
}
}
图片上传(ssh)
猜你喜欢
转载自ljm1227134894.iteye.com/blog/1889352
今日推荐
周排行