JAVA 上传图片代码 要MVC模式的 SSH框架集成的!!!谢谢了 20
展开全部
页面:
<form action="" method="post" enctype="multipart/form-data">
<input type="file"/>
</form>
后台action:
String dirPath =request.getSession().getServletContext().getRealPath("/") + "upload\\";
//判断路径是否存在,如果不存在,就创建这个路径
File flFile=new File(dirPath);
if(!flFile.exists()){
flFile.mkdir();
}
//form.getMultipartRequestHandler()获取文件并存储
Hashtable fileh = form.getMultipartRequestHandler().getFileElements();
for (Enumeration e = fileh.keys(); e.hasMoreElements();) {
String key = (String) e.nextElement();
try {
FormFile formfile = (FormFile) fileh.get(key);
String filename = formfile.getFileName().trim(); //待上传文件的名字
InputStream ins = formfile.getInputStream();
OutputStream os = new FileOutputStream(dirPath + File.separatorChar +filename);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();
}
}
catch (Exception ex) {
}
}
<form action="" method="post" enctype="multipart/form-data">
<input type="file"/>
</form>
后台action:
String dirPath =request.getSession().getServletContext().getRealPath("/") + "upload\\";
//判断路径是否存在,如果不存在,就创建这个路径
File flFile=new File(dirPath);
if(!flFile.exists()){
flFile.mkdir();
}
//form.getMultipartRequestHandler()获取文件并存储
Hashtable fileh = form.getMultipartRequestHandler().getFileElements();
for (Enumeration e = fileh.keys(); e.hasMoreElements();) {
String key = (String) e.nextElement();
try {
FormFile formfile = (FormFile) fileh.get(key);
String filename = formfile.getFileName().trim(); //待上传文件的名字
InputStream ins = formfile.getInputStream();
OutputStream os = new FileOutputStream(dirPath + File.separatorChar +filename);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();
}
}
catch (Exception ex) {
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询