<form action="/StrutsFileUpAndDown/register.do" method="post" enctype="multipart/form-data"> 名字:<input type="text" name="name" /> 头像:<input type="file" name="file"/> <input type="submit" value="注册用户"> </form>
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.yourcompany.struts.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
/**
* MyEclipse Struts
* Creation date: 08-24-2017
*
* XDoclet definition:
* @struts.form name="userForm"
*/
public class UserForm extends ActionForm {
/*
* Generated Methods
*/
private String username;
private FormFile file;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public FormFile getFile() {
return file;
}
public void setFile(FormFile file) {
this.file = file;
}
}
UserForm userForm = (UserForm) form; String username = userForm.getUsername(); FormFile file = userForm.getFile(); //通过formFile可以获取关于用户上传文件的各种信息 //用于获取文件名字 String fileName = file.getFileName(); //用于获取文件大小 int fileSize = file.getFileSize();
try {
//获取输入流
is = file.getInputStream();
//得到输出流
//1.得到file文件夹,上传到tomcat服务器后的绝对路径(file文件为新创建的文件夹)
String filePath = this.getServlet().getServletContext().getRealPath("/file");
//两个"//"的其中一个"/"为转义符
os=new FileOutputStream(filePath+"\"+fileName);
int len=0;//表示读取的字节
//做一个缓存,防止文件过大而造成错误
byte[] buff=new byte[1024];
while((len=is.read(buff))!=-1)
{
os.write(buff,0,len);
}
is.close();
os.close();
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有