web项目中图片上传
来源:互联网 发布:sftp 批量下载 java 编辑:程序博客网 时间:2024/05/16 11:46
最近一直在做一个项目,但是有好多的东西都是第一次碰到,上网上查了很多资料,但是由于基础薄弱,只是堪堪了解一点。我所想实现的是,在后台页面上传图片,存放路径到数据库中,而前台页面通过数据库可以查到,然后在页面显示,而上传的图片要保存到我在项目中所建的文件夹内,服务器上有临时存放的位置String root = ServletActionContext.getServletContext().getRealPath("/upload");但是服务器清除的时候会把这些上传的文件就给清除了。我现在做到的是,通过拷贝临时文件夹内的文件,保存到我所建的文件夹内,在这里记下以防止我遗忘。所用ssi框架,struts2,spring,mybatis,数据库orca,服务器tomcat。1,前台页面
<form action="uploads" method="post" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit"> </form>发送
action请求到配置文件struts.xml里寻找
<action name="uploads" class="net.test.Controllers" method="upload"> <result name="testUpload" ype="redirect">/file.jsp</result> </action>
public String upload() throws IOException { // 得到文件存放的临时路径 String root = ServletActionContext.getServletContext().getRealPath( "/upload"); System.out.println("那这个呢" + root); InputStream is = new FileInputStream(file); // 原file文件 File dest = new File(root, fileFileName);// 服务器的文件 OutputStream os = new FileOutputStream(dest); // FileOutputStream fot=new FileOutputStream("D://"); // while(is.read()>-1){ // fot.write(b); // } System.out.println("fileFileName: " + fileFileName); // 因为file是存放在临时文件夹的文件,我们可以将其文件名和文件路径打印出来,看和之前的fileFileName是否相同 System.out.println("file: " + file.getName()); System.out.println("file: " + file.getPath()); byte[] buffer = new byte[500]; int length = 0; while (-1 != (length = is.read(buffer, 0, buffer.length))) { os.write(buffer); } os.close(); is.close(); // 通过JVM读取java.io.tmpdir属性取得临时文件夹 // File targetDir = new File(System.getProperty("java.io.tmpdir")); String root1 = this.getClass().getClassLoader().getResource("") .getPath(); System.out.println("打印这个" + root1); // 获得文件路径 String fileuil = "D://yzg_new//youtuWeb//WebContent//upload"; haha = fileuil + fileFileName; // 拷贝文件(a,b)a到b FileUtils.copyFileToDirectory(dest, new File(fileuil));// 完成了文件的拷贝工作 // String file_real_path= ServletContext.getRealPath("mypath/filename"); return "testUpload"; }
在项目中新建upload文件夹,上传成功后刷新一下文件就会发现文件出现在目录里。
然后问题出现了,我放进数据库里因该是什么路径呢?那前台显示的呢?有经验的朋友看到的话,不妨给我讲一下,麻烦了。如果不懂得朋友有什么新收获,一块来研究一下吧。
0 0
- web项目中图片上传
- java web项目图片上传
- java web项目中上传文件、图片路径问题
- 上传图片上传项目源码中
- java web项目中上传文件、图片、freemarker生成静态页面路径问题
- web项目中,上传到tomcat的图片被删除的原因
- 由web项目中上传图片所引出的路径问题
- WEB中上传图片即使显示
- winform实现为web项目上传图片的功能
- 关于web项目中的图片上传、并显示问题
- 关于web项目中的图片上传、并显示问题
- web app项目使用uploadifive插件上传图片
- web项目静态资源诸如图片等上传引用问题
- java web项目中读取本地图片
- java web上传图片
- Web Api 图片上传
- web图片上传
- web图片或者上传
- iOS 中 AFNetworking HTTPS 的使用
- 实现JSP分页方法一:手写
- Java URL处理
- 基于能量的生成对抗网络
- 设置EditText自动失去焦点
- web项目中图片上传
- Android视频框架 Vitamio 打造自己的万能播放器
- c#设计模式-状态模式(1)
- Iptables模块recent应用
- qt中pro文件写法
- 关于ListView的addHeaderView(View) 方法
- pod install后出现: [!] `<PBXResourcesBuildPhase UUID=`x`>` attempted to initialize an object with an...
- 什么是JS事件冒泡?(网络)
- 对自己的要求