您的位置:首页 > Web前端 > JavaScript

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

2017-07-15 20:35 726 查看
用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类型
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息