jsp文件上传步骤

  1. 导入jar包(commons-io-2.4.jar),(commons-fileupload-1.2.2.jar)

    表单属性

1
enctype="multipart/form-data"

jsp页面如下

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
90
91
92
93
94
95
96
97
98
99
100
101
102
public void (HttpServletRequest request, HttpServletResponse response) throws IOException{
PrintWriter out = response.getWriter();
String fieldName="";
String defaultPic="default.jpg";
NewsDetail news=new NewsDetail();
NewsDetailImpl detail = new NewsDetailImpl();
if(ServletFileUpload.isMultipartContent(request)){
FileItemFactory factory=new DiskFileItemFactory();
ServletFileUpload upload=new ServletFileUpload(factory);
List<FileItem> FileitemLsit = null;
try {
FileitemLsit = upload.parseRequest(request);
} catch (FileUploadException e) {
e.printStackTrace();
}
for(FileItem item:FileitemLsit){
if(item.isFormField()){
fieldName=item.getFieldName();
if (fieldName.equals("slt")) {
news.setCategoryId(Integer.parseInt(item.getString(code)));
}
if (fieldName.equals("title")) {
news.setTitle(item.getString(code));
}
if (fieldName.equals("author")) {
news.setAuthor(item.getString(code));
}
if (fieldName.equals(大专栏  jsp文件上传步骤pan class="string">"summary")) {
news.setSummary(item.getString(code));
}
if (fieldName.equals("content")) {
news.setContent(item.getString(code));
}
}else{//文件域
String fileName=item.getName();
if(item.getSize()>0){
if(item.getSize()<1024*2000){
//判断是否是图片:gif,png,jpeg,jpg
if(fileName.endsWith(".gif")|fileName.endsWith(".png")|fileName.endsWith(".jpeg")
|fileName.endsWith(".jpg")){
//获取系统时间,精确毫秒,随机数+"扩展名"
String suffix=fileName.substring(fileName.lastIndexOf("."));
//查询所有图片名字,如果不重复进行下一步
if(detail.selectPicName()!=null&&detail.selectPicName().add(fileName)){
String date=new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss_SSS").format(new Date());
Random random=new Random();
int rd=random.nextInt(99999);
File fullFile=new File(fileName);
File newfullFile=new File(date+rd+suffix);
fullFile.renameTo(newfullFile);
//获取服务器物理uoload/全路径
String uploadPath=this.getServletContext().getRealPath("upload/");
File ServeruploadPath=new File(uploadPath);
if(!ServeruploadPath.exists()){
ServeruploadPath.mkdirs();
}
File uploadFile=new File(uploadPath+"/"+newfullFile.getName());

try {
item.write(uploadFile);
} catch (Exception e) {
e.printStackTrace();
}
news.setPicPath(newfullFile.getName());

}else{
out.print("<script>alert('文件名上传失败请重试!'); location.href='NewsDetailServlet?i=3'</script>");
out.flush();
out.close();
return;
}

}else{
out.print("<script>alert('文件格式不正确!'); location.href='NewsDetailServlet?i=3'</script>");
out.flush();
out.close();
return;
}

}else{
out.print("<script>alert('文件太大了!'); location.href='NewsDetailServlet?i=3'</script>");
out.flush();
out.close();
return;
}
}else{
news.setPicPath(defaultPic);
}
}
}
}
int i = detail.add(news);
if(i!=-1&&i!=0){
out.print("<script>alert('添加成功!'); location.href='NewsDetailServlet?i=3'</script>");
out.flush();
out.close();
}else{
out.print("<script>alert('添加失败!'); location.href='NewsDetailServlet?i=3'</script>");
out.flush();
out.close();
}
}

猜你喜欢

转载自www.cnblogs.com/liuzhongrong/p/12346081.html
今日推荐