JSP servlet实现文件上传下载和删除
本文实例为大家分享了android九宫格图片展示的具体代码,供大家参考,具体内容如下
由于存储空间,对一般用户而言,就是用来操作存储文件的,所以这两天,特意看了一下windows下用servlet实现文件上传、下载和删除,下面是详细代码说明
上传:
用的是commons-fileupload-1.2.2.jar和commons-io-2.0.1.jar组件,可以去apache官网上去下载,然后放到webroot/web-inf/lib目录下
upload.html
<html> <head> <meta http-equiv="content-type" content="text/html; charset=gbk"> <title>file upload</title> </head> <body> <!-- action="fileupload"对应web.xml中<servlet-mapping>中<url-pattern>的设置.--> <!-- 必须是multipart的表单数据,才能完整的传递文件数据 --> <form name="myform" action="fileupload" method="post" enctype="multipart/form-data"> file:<br> <input type="file" name="myfile"><br> <br> <input type="submit" name="submit" value="commit"> </form> </body> </html>
web.xml里加上下面几行:
<servlet> <servlet-name>upload</servlet-name> <servlet-class>am.demo.upload</servlet-class> </servlet> <servlet-mapping> <servlet-name>upload</servlet-name> <url-pattern>/fileupload</url-pattern> </servlet-mapping>
src目录下新建文件upload.java:
package am.demo; import java.io.file; import java.io.ioexception; import java.util.iterator; import java.util.list; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import org.apache.commons.fileupload.fileitem; import org.apache.commons.fileupload.disk.diskfileitemfactory; import org.apache.commons.fileupload.servlet.servletfileupload; @suppresswarnings("serial") public class upload extends httpservlet { private string uploadpath = "d://temp"; // 上传文件的目录 @suppresswarnings("unchecked") public void dopost(httpservletrequest request, httpservletresponse response) throws ioexception, servletexception { try { // create a factory for disk-based file items diskfileitemfactory factory = new diskfileitemfactory(); // create a new file upload handler servletfileupload upload = new servletfileupload(factory); list<fileitem> items = upload.parserequest(request);// 得到所有的文件 iterator<fileitem> i = items.iterator(); while (i.hasnext()) { fileitem fi = (fileitem) i.next(); string filename = fi.getname(); if (filename != null) { file fullfile = new file(fi.getname()); file savedfile = new file(uploadpath, fullfile.getname()); fi.write(savedfile); } } response.setcontenttype("text/html;charset=gbk"); response.getwriter().print( "<mce:script language='javascript'><!-- alert('上传成功');window.location.href='index.jsp'; // --></mce:script>"); } catch (exception e) { // 可以跳转出错页面 e.printstacktrace(); } } public void init() throws servletexception { file uploadfile = new file(uploadpath); if (!uploadfile.exists()) { uploadfile.mkdirs(); } } }
再看下载downlaod.java:
package am.demo; import java.io.file; import java.io.fileinputstream; import java.io.ioexception; import java.io.outputstream; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; @suppresswarnings("serial") public class download extends httpservlet { public void doget(httpservletrequest request, httpservletresponse response) throws ioexception, servletexception { string afilename = new string(request.getparameter("name").getbytes( "iso8859-1"), "gbk"); file fileload = new file("d:/temp", afilename); fileinputstream in = null; // 输入流 outputstream out = response.getoutputstream(); byte b[] = new byte[1024]; try { response.setcontenttype("application/x-msdownload;"); response.setheader("content-disposition", "attachment; filename=" + new string(afilename.getbytes("gbk"), "iso-8859-1")); // download the file. in = new fileinputstream(fileload); int n = 0; while ((n = in.read(b)) != -1) { out.write(b, 0, n); } } catch (throwable e) { e.printstacktrace(); } finally { try { in.close(); out.close(); } catch (throwable e) { e.printstacktrace(); } } } public void dopost(httpservletrequest request, httpservletresponse response) throws ioexception, servletexception { doget(request, response); } }
删除delete.java:
package am.demo; import java.io.file; import java.io.filenotfoundexception; import java.io.ioexception; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; @suppresswarnings("serial") public class delete extends httpservlet { public void doget(httpservletrequest request, httpservletresponse response) throws filenotfoundexception, ioexception { string afilename = new string(request.getparameter("name").getbytes( "iso8859-1"), "gbk"); file file = new file("d:/temp", afilename); response.setcontenttype("text/html;charset=gbk"); if (!file.isdirectory()) { file.delete(); response.getwriter().print( "<mce:script language='javascript'><!-- alert('删除成功');window.location.href='index.jsp'; // --></mce:script>"); } else { } } public void dopost(httpservletrequest request, httpservletresponse response) throws ioexception, servletexception { doget(request, response); } }
因为ubuntu server是黑屏,本人用的是ubuntu server10.04,不方便用浏览器来查看文件,因为没有图形界面,也不方便调试,所以可以先在windows上测试通过,然后把相应文件拷到ubuntu server中的tomcat相应目录下,可以通过samba来传文件,但遇到一个问题,就是中文乱码的问题,因为在开始安装时,选的是英文,后面用网上的一些办法,还是没有解决中文乱码的问题,有知道的大侠,还烦请转告。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持硕编程。
相关文章
- jsp+servlet实现文件上传与下载功能
- EJB3.0部署消息驱动Bean抛javax.naming.NameNotFoundException异常
- 在JSP中使用formatNumber控制要显示的小数位数方法
- 秒杀系统Web层设计的实现方法
- 将properties文件的配置设置为整个Web应用的全局变量实现方法
- JSP使用过滤器防止Xss漏洞
- 在JSP页面中动态生成图片验证码的方法实例
- 详解JSP 内置对象request常见用法
- 使用IDEA编写jsp时EL表达式不起作用的问题及解决方法
- jsp实现局部刷新页面、异步加载页面的方法
- Jsp中request的3个基础实践
- JavaServlet的文件上传和下载实现方法
- JSP页面的静态包含和动态包含使用方法