多部件表单(图片上传)

来源:互联网 发布:淘宝拉黑退款率高的人 编辑:程序博客网 时间:2024/05/18 02:27

1.        导入上传图片的jar包

1)        commons-fileupload-1.2.2.jar

2)        commons-io-2.4.jar


2. 将表单设置成多部件表单的类型

在页面form中添加enctype="multipart/form-data"属性


3.

public News fileUpload(News n,File pic,String picFileName) {
if(picFileName!=null){
String  newName=UUID.randomUUID().toString()+picFileName.substring(picFileName.lastIndexOf("."));
File newFile=new File("D:/apache-tomcat-7.0.75/webapps/img/"+newName);
try {
FileUtils.copyFile(pic, newFile);
} catch (IOException e) {
e.printStackTrace();
}
n.setPic("../img/"+newName);
}
return n;
}

原创粉丝点击