struts2 上传文件
来源:互联网 发布:csgo画面优化设置 编辑:程序博客网 时间:2024/06/03 16:02
1. 随即生成 路径的文件夹,属于共用方法
import java.io.File;public class UploadUtil {public static String makeDirs(String storeDirectory, String fileName) {int hashCode = fileName.hashCode();int dir1 = hashCode&0xf;int dir2 = (hashCode&0xf0)>>4;String newPath = dir1+"/"+dir2;File directory = new File(storeDirectory,newPath);if(!directory.exists())directory.mkdirs();newPath=storeDirectory+directory.separator+dir1+directory.separator+dir2;return newPath;}}
2 action 层
// 文件上传private File upload;private String uploadFileName;private String uploadContentType;private InputStream fileStream;private OutputStream out;
public String save(){ String storeDirectory=ServletActionContext.getServletContext().getRealPath("/file");try { fileStream = new FileInputStream(upload); String uri=UploadUtil.makeDirs(storeDirectory, uploadFileName); File f=new File(uri); if(f.exists()){ out=new FileOutputStream(uri+f.separator+uploadFileName); } int len=-1; byte[] b=new byte[1024]; while((len=fileStream.read(b))!=-1){ out.write(b, 0, len); } model.setImage(uri); model.setImageurl(uploadFileName); hbase.save(model);} catch (Exception e) {e.printStackTrace();}finally{if(fileStream!=null)try {fileStream.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}if(out!=null)try {out.close();} catch (IOException e) {e.printStackTrace();}}return SUCCESS;}
3.jsp 页面 (upload 要与action 的 private File upload; 名字保持一致 )
<tr><td>图片:<input type="file" name="upload"/><br/></td></tr>
4. 上传文件 是sturts 的拦截器 功能, 自定拦截器或者 使用默认的只要 继承了 extends="struts-default" 即可。
- struts2上传文件(三) struts2上传文件
- [Struts2]Struts2的文件上传
- Struts2的文件上传
- Struts2文件上传
- Struts2 上传文件实例
- struts2文件上传
- struts2文件上传
- Struts2的文件上传
- struts2-上传文件(1)
- struts2-上传文件(2)
- Struts2上传文件示例
- struts2文件上传
- Struts2实现文件上传
- struts2文件上传
- Struts2 文件上传
- ext + struts2 上传文件
- Struts2 文件上传
- Struts2的文件上传
- 苹果AppStore应用商店生存之道:国内iOS开发者创业经验分享
- Chrome终极全屏模式Kiosk
- Oracle层级询语句connect by 用法详解
- 黑马程序员:StringBuffer与StringBuilder的介绍及区别
- UVa 105 The Skyline Problem (想法题)
- struts2 上传文件
- 快要 弱冠之年
- C指针参数传递
- Java:对象的强、软、弱和虚引用
- Android UI系列:关于按钮点击事件
- 上大学的四年---以此结束四年的时光
- diff 比较文件差集
- 动画加载方式
- muduo网络库学习之EventLoop(六):TcpConnection::send()、shutdown()、handleRead()、handleWrite()