编辑上传文件的页面upload.html
注意事项:上传方式使用post不能使用get(get不能上传文件)
表单 enctype 属性应该设置为 multipart/form-data.(表示提交的数据是二进制文件)
upload.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<!doctype html>
<html>
<head>
<meta charset= "utf-8" >
<title>文件上传</title>
</head>
<body>
<form action= "uploadphotoservlet" method= "post" enctype= "multipart/form-data" >
人物名称:<input type= "text" name= "heroname" /><br>
上传头像:<input type= "file" name= "filepath" /><br>
<input type= "submit" value= "上传" >
</form>
</body>
</html>
|
uploadptotoservlet文件上传类--上传功能的开发
将commons-io-1.4.jar和commons-fileupload-1.2.2.jar 两个jar包放到web-inf/lib 目录下。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
import java.io.file;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.io.inputstream;
import java.io.printwriter;
import java.util.iterator;
import java.util.list;
import javax.servlet.servletexception;
import javax.servlet.annotation.webservlet;
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.fileuploadexception;
import org.apache.commons.fileupload.disk.diskfileitemfactory;
import org.apache.commons.fileupload.servlet.servletfileupload;
/**
* servlet implementation class uploadphotoservlet
*/
@webservlet ( "/uploadphotoservlet" )
public class uploadphotoservlet extends httpservlet {
private static final long serialversionuid = 1l;
/**
* @see httpservlet#httpservlet()
*/
public uploadphotoservlet() {
super ();
// todo auto-generated constructor stub
}
/**
* @see httpservlet#doget(httpservletrequest request, httpservletresponse response)
*/
protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
// todo auto-generated method stub
// response.getwriter().append("served at: ").append(request.getcontextpath());
string filename= null ;
diskfileitemfactory factory= new diskfileitemfactory(); //磁盘文件条目工厂
servletfileupload upload= new servletfileupload(factory); //负责处理上传的文件数据,并将表单中每个输入项封装成一个fileitem对象中
//设置上传文件的大小为10m
factory.setsizethreshold( 2 * 1024 * 1024 );
list items= null ;
try {
//parse 解析
items=upload.parserequest(request); //得到一个保存了所有上传内容的list对象
} catch (fileuploadexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
iterator iter=items.iterator(); //迭代上传的文件数据
while (iter.hasnext()){
fileitem item=(fileitem) iter.next();
if (!item.isformfield()){ //如果不是上传的
//根据时间戳创建头像文件
filename=system.currenttimemillis()+ ".jpg" ;
//通过getrealpath获取上传文件夹,如果项目存在将存在当前项目下 不存在的话创建项目文件夹
//图片文件夹
string photofolder=request.getservletcontext().getrealpath( "uploaded" );
file f= new file(photofolder,filename);
f.getparentfile().mkdirs(); //如果父文件夹不存在则自动创建
//通过item.getinputstream() 获取浏览器上传的文件
inputstream is = item.getinputstream(); //将文件读进来
//复制文件
fileoutputstream fos= new fileoutputstream(f); //往界面上显示
byte [] b= new byte [ 2 * 1024 * 1024 ];
int len= 0 ;
while ((len=is.read(b))!=- 1 ){
fos.write(b, 0 , len);
}
fos.close();
} else {
system.out.println(item.getfieldname()); //heroname
string value=item.getstring();
value= new string(value.getbytes( "iso-8859-1" ), "utf-8" );
system.out.println(value); //桑葚
}
}
string html= "<img width='200' height='150' src='uploaded/%s'/>" ;
response.setcontenttype( "text/html" );
printwriter pw=response.getwriter();
pw.format(html, filename);
}
/**
* @see httpservlet#dopost(httpservletrequest request, httpservletresponse response)
*/
protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
// todo auto-generated method stub
doget(request, response);
}
}
|
运行结果:
总结
以上所述是小编给大家介绍的j2ee servlet上传文件到服务器并相应显示功能的实现代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:https://www.cnblogs.com/gaohuanhuan/archive/2018/07/06/9271718.html