1首先建立文件上传jsp页面如下
<form action="" method="post" enctype="multipart/form-data" > 文件<input type="file" name="upload"> <input type="submit" class="button" value="添加"/> </form>
2.action处理上传业务
public class fileupload(){
private File upload;
private String uploadFileName;//struts根据表单name属性自动获取上传的文件名 private String uploadContentType;//struts根据表单name属性自动获取上传的文件属性
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
public String Insert() throws IOException{
String path= "D:/workspace/.metadata/.me_tcat7/webapps/EducationWebsite/upload/";//指定文件保存路径(在此保存在wbsapps文件中是为了在不进行刷新的情况下图面能及时显现到页面中)
System.out.println(path);
FileInputStream fis=new FileInputStream(upload);
FileOutputStream fos=new FileOutputStream(path+uploadFileName);
int data = 0;
while((data=fis.read())!=-1){
fos.write(data);
}
fis.close();
fos.close();
cou.setPicture(uploadFileName);
}
3.xml文件中配置上传文件的最大容量:
<constant name="struts.multipart.maxSize" value="1024000000"/>//配置上传文件的最大值10G