用jsp实现简单的图片上传功能

1 先做一个页面,选择上传的图片

<body><form action="uploadServlet" enctype="multipart/form-data" method="POST" >selectimage: <input type="file" name="myfile"/><br><input type="submit" value="upload"/></form>
</body>

注意要以enctype="multipart/form-data" 编码形式来提交

2 在转到的servlet读取到传过来的内容,并截取出图片的信息,建一个文件,然后把信息保存进去,实现了图片的上传。

package com.zhou.action;
import Java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Date;importjavax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public classUploadServlet extends HttpServlet {publicUploadServlet() {super();
}public voiddestroy() {super.destroy(); // Just puts "destroy" string in log// Put your code here
}public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {String contentType=request.getContentType();String servername=request.getServerName();String realpath=request.getRealPath(servername);System.out.println(contentType);InputStream in=null;OutputStream out=null;if(contentType.indexOf("multipart/form-data")>=0){in=request.getInputStream();int formlength=request.getContentLength();byte[] formcontent=new byte[formlength];int totalread=0;int nowread=0;while(totalread<formlength){nowread=in.read(formcontent,totalread, formlength);totalread+=nowread;}String strcontent=new String(formcontent);System.out.println(strcontent);int typestart=strcontent.indexOf("Content-Type:")+14;int typeend=strcontent.indexOf("\n", typestart)-1;String formType=strcontent.substring(typestart, typeend);if(formType.equals("image/jpeg")||formType.equals("image/gif")||formType.equals("image/pjepg")){int filenamestart=strcontent.indexOf("filename=\"")+10;int filenameend=strcontent.indexOf("\n",filenamestart)-2;String filename=strcontent.substring(filenamestart,filenameend);filename=filename.substring(filename.lastIndexOf("."));String newfilename=""+(newDate()).getDate()+(new Date()).getHours()+(new Date()).getMinutes()+(newDate()).getSeconds();newfilename=newfilename+filename;realpath=realpath+"";newfilename=realpath+newfilename;int filestart=strcontent.indexOf("\n",typestart)+1;filestart=strcontent.indexOf("\n",filestart)+1;int intboundary=contentType.indexOf("boundary=")+10;String strboundary=contentType.substring(intboundary);int fileend=strcontent.indexOf(strboundary,filestart)-4;String saveFile=strcontent.substring(filestart,fileend);int contentstart=strcontent.substring(0,filestart).getBytes().length;int contentend=strcontent.substring(0,fileend).getBytes().length;System.out.println(saveFile);File myfile=new File(realpath);if(!myfile.exists()){myfile.mkdirs();}out=new FileOutputStream(newfilename);out.write(formcontent, contentstart,contentend-contentstart);response.sendRedirect("show.jsp");}else{response.sendRedirect("error.jsp");}}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request,response);
}public void init() throws ServletException {// Put your code here
}}

3 配置好xml里的servlet,当然可以在建servlet配置。

<servlet><description>This is the description of my J2EEcomponent</description><display-name>This is the display name of my J2EEcomponent</display-name><servlet-name>uploadServlet</servlet-name><servlet-class>com.zhou.action.UploadServlet</servlet-class>
</servlet><servlet-mapping><servlet-name>uploadServlet</servlet-name><url-pattern>/uploadServlet</url-pattern>
</servlet-mapping>

4 部署运行程序,可以在你的部署目录下的localhost下upload里面看到你上传的图片了。

表单中enctype=“multipart/form-data”的意思,是设置表单的MIME编码 
默认情况,这个编码格式是application/x-www-form-urlencoded,可以通过request.getParameter来获取表单中的内容 
但是文件上传需要接受的是二进制的数据需要使用multipart/form-data,才能完整的传递文件数据,进行下面的操作 
使用了此设置,就不能利用getParameter直接获取文本内容了,而是用一个字节数组来接收内容,然后再转换成String类型

用jsp实现简单的图片上传功能相关推荐

  1. alert()的功能_前端实现简单的图片上传小图预览功能

    <!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8" ...

  2. python自动上传图片_使用Python实现一个简单的图片上传存储服务

    使用flask实现的一个简单的图片上传服务 设计初衷 对于图片的存储,有很多选择,一般采用云服务如(七牛,又拍等),但是国内的服务像七牛 自定义域名竟然需要域名备案(Excuse me,当初就是因为备 ...

  3. 用go来搭建一个简单的图片上传网站

    提前说明一下:代码参考了<Go语言编程>,稍有变动, 自己亲自玩了一遍. 之前玩过go web server, 现在来用go来搭建一个简单的图片上传网站, 工作目录是:~/photoweb ...

  4. 一个.net6简单的图片上传(文件也可用)

    #一个.net6简单的图片上传(文件也可用) ##页面 @{ViewData["Title"] = "图片上传Demo"; }<form asp-acti ...

  5. (转)淘淘商城系列——实现图片上传功能

    http://blog.csdn.net/yerenyuan_pku/article/details/72808000 上文我们使用FastDFS-Client进行了简单的文件上传操作测试,淘淘商城项 ...

  6. PHP实现图片上传功能

    PHP实现图片上传功能: 第一个页: picupload.php 代码如下: <form action="picop.php"  method="post" ...

  7. ckfinder php 配置,PHP中Ckeditor+Ckfinder配置图片上传功能_PHP教程

    从标题来看我们知道Ckeditor不支持图片上传功能,它是需要一个组件Ckfinder才可以支持上传图片, 本文章就来详细的介绍了如何配置Ckeditor+Ckfinder实现图片上传的功能. 第一: ...

  8. 纯前端实现图片上传功能

    纯前端实现图片上传功能,告别后端formData上传 使用ElementUI中的upload组件+腾讯云实现简单的图片上传功能 了解了upload的基本属性之后我们要开始上硬菜了 使用ElementU ...

  9. ckeditor java 上传_java使用CKEditor实现图片上传功能

    java如何使用ckeditor实现图片上传功能,具体内容如下 1.根据实际需要下载指定的ckeditor 2.删除文件ckeditor/plugins/image/dialogs/image.js预 ...

最新文章

  1. VC++调试程序、快捷键以及Debug版本与Release版本
  2. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 按钮:按钮组
  3. CLion:JetBrains 正式推出的 C/C++ IDE
  4. 兴义高考八中2021成绩查询,兴义第八中学2021年录取分数线
  5. 拉普拉斯分布_中心极限定理:从高尔顿板到麦克斯韦分布
  6. 免费的精品: Productivity Power Tools 动画演示
  7. python文件合法模式组合_以下选项中,不是Python文件二进制打开模式的合法组合是...
  8. Linux 搭建PHP环境
  9. leetcode python3 简单题136. Single Number
  10. IBM 推出全球首台计算性能最强悍的 53 位量子计算机
  11. 扩展Jquery方法创建LigerUI Grid
  12. python可以做什么-Python简直就是万能的,你用Python都做过哪些事?
  13. [Bochs]Bochs调试技术
  14. Python内置下载服务器
  15. 利用python打乱xlsx表格
  16. 舒老师的hu测(日常吐槽)
  17. 《东周列国志》第十四回 卫侯朔抗王入国 齐襄公出猎遇鬼
  18. Linux之ubuntu离线安装软件包
  19. 未来人工智能产品的思维,主要有哪几个趋势
  20. 苹果手机怎么编辑word文档_多人协同编辑一份Word文档用修订功能就对了

热门文章

  1. 政府采购项目数据抓取
  2. 与OA系统的固定资产管理相比,固定资产管理云系统的优势在哪里?
  3. sqlserver 列转行逗号分隔
  4. 基于python+opencv的网球识别
  5. 小米集团Q3收入781亿元,调整后净利盈利52亿,同比上涨25.4%
  6. nginx关闭目录浏览功能
  7. 英语学习详细笔记(十九)介系词
  8. mysql extendedstatus_mysqladmin的extended-status指令查看 mysql各状态
  9. 如何在Chrome浏览器中关闭cookie
  10. 一路去**ddss第6天