用jsp实现简单的图片上传功能
2017-07-15 20:35
726 查看
用jsp实现简单的图片上传功能
1 先做一个页面,选择上传的图片
注意要以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类型
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实现简单的图片上传功能
- 用jsp实现简单的图片上传功能(multipart/form-data形式的表单)
- 用jsp实现简单的图片上传功能
- jsp中点击图片弹出文件上传界面及预览功能的实现
- 简单实现JS上传图片预览功能
- 简单实现jQuery上传图片显示预览功能
- java+ jsp+js 实现富文本编辑和上传图片功能
- Servlet+Jsp实现图片或文件的上传功能具体思路及代码
- jsp实现图片上传功能
- 一个简单实现了PHP图片上传功能
- sql server 关于表中只增标识问题 C# 实现自动化打开和关闭可执行文件(或 关闭停止与系统交互的可执行文件) ajaxfileupload插件上传图片功能,用MVC和aspx做后台各写了一个案例 将小写阿拉伯数字转换成大写的汉字, C# WinForm 中英文实现, 国际化实现的简单方法 ASP.NET Core 2 学习笔记(六)ASP.NET Core 2 学习笔记(三)
- Servlet+Jsp实现图片或文件的上传功能具体思路及代码
- Servlet+Jsp实现图片或文件的上传功能
- Servlet+Jsp实现图片或文件的上传功能具体思路及代码
- ueditor1_4_3-utf8-jsp 配置实现上传图片的功能
- Servlet+Jsp实现图片或文件的上传功能具体思路及代码
- jsp中点击图片弹出文件上传界面及预览功能的实现
- 用jsp实现图片上传功能!
- 简单实现JS上传图片预览功能