jsp和servlet上传问题用的是common-fileupload
<formaction="Minsert?do=3"method="post"enctype="multipart/form-data"><inputtype="text...
<form action="Minsert?do=3" method="post" enctype="multipart/form-data">
<input type="text" name="mytext" id="mytext">
<input type="file" name="file1">
<input type="file" name="file2"/>
<input type="submit" value="确定">
</form>
servlet代码怎么写?我要上传两个 展开
<input type="text" name="mytext" id="mytext">
<input type="file" name="file1">
<input type="file" name="file2"/>
<input type="submit" value="确定">
</form>
servlet代码怎么写?我要上传两个 展开
1个回答
展开全部
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
2 <%
3 String path = request.getContextPath();
4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
5 %>
6
7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
8 <html>
9 <head>
10 <base href="<%=basePath%>">
11
12 <title>My JSP 'filesUpload.jsp' starting page</title>
13
14 </head>
15
16 <body>
17 <form action="<%=basePath%>servlet/FileUploadServlet"
enctype="multipart/form-data" method="post">
18 <input type="file" name="file1"><br>
19 <input type="file" name="file2"><br>
20 <input type="file" name="file3"><br>
21 <input type="submit">
22 </form>
23 </body>
24 </html>
package experiment4;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.List;
6
7 import javax.servlet.ServletException;
8 import javax.servlet.http.HttpServlet;
9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11
12 import org.apache.commons.fileupload.FileItem;
13 import org.apache.commons.fileupload.FileUploadException;
14 import org.apache.commons.fileupload.FileUploadBase.FileSizeLimitExceededException;
15 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
16 import org.apache.commons.fileupload.servlet.ServletFileUpload;
17
18 public class FileUploadServlet extends HttpServlet {
19
20 public void doPost(HttpServletRequest request, HttpServletResponse response)
21 throws ServletException, IOException {
22 request.setCharacterEncoding("utf-8");//防止中文名乱码
23 int sizeThreshold=1024*6; //缓存区大小
24 String basePath = this.getServletContext().getRealPath("/upload/");
25 File repository = new File(basePath); //缓存区目录
26 long sizeMax = 1024 * 1024 * 2;//设置文件的大小为2M
27 final String allowExtNames = "jpg,gif,bmp,rar,rar,txt,docx";
28 DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
29 diskFileItemFactory.setRepository(repository);
30 diskFileItemFactory.setSizeThreshold(sizeThreshold);
31 ServletFileUpload servletFileUpload=new ServletFileUpload(diskFileItemFactory);
32 servletFileUpload.setSizeMax(sizeMax);
33
34 List<FileItem> fileItems = null;
35 try{
36 fileItems = servletFileUpload.parseRequest(request);
37
38 for(FileItem fileItem:fileItems){
39 long size=0;
40 String filePath = fileItem.getName();
41 System.out.println(filePath);
42 if(filePath==null || filePath.trim().length()==0)
43 continue;
44 String fileName=filePath.substring(filePath.lastIndexOf(File.separator)+1);
45 // String fileName=String.valueOf(System.currentTimeMillis());
46 String extName=filePath.substring(filePath.lastIndexOf(".")+1);
47 // fileName+="."+extName;
48 if(allowExtNames.indexOf(extName)!=-1)
49 {
50 try {
51 fileItem.write(new File(basePath+File.separator+fileName));
52 } catch (Exception e) {
53 e.printStackTrace();
54 }
55 }
56 else{
57 throw new FileUploadException("file type is not allowed");
58 }
59 }
60 }catch(FileSizeLimitExceededException e){
61 System.out.println("file size is not allowed");
62 }catch(FileUploadException e1){
63 e1.printStackTrace();
64 }
65
66
67 }
68
69 }
70
2 <%
3 String path = request.getContextPath();
4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
5 %>
6
7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
8 <html>
9 <head>
10 <base href="<%=basePath%>">
11
12 <title>My JSP 'filesUpload.jsp' starting page</title>
13
14 </head>
15
16 <body>
17 <form action="<%=basePath%>servlet/FileUploadServlet"
enctype="multipart/form-data" method="post">
18 <input type="file" name="file1"><br>
19 <input type="file" name="file2"><br>
20 <input type="file" name="file3"><br>
21 <input type="submit">
22 </form>
23 </body>
24 </html>
package experiment4;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.List;
6
7 import javax.servlet.ServletException;
8 import javax.servlet.http.HttpServlet;
9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11
12 import org.apache.commons.fileupload.FileItem;
13 import org.apache.commons.fileupload.FileUploadException;
14 import org.apache.commons.fileupload.FileUploadBase.FileSizeLimitExceededException;
15 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
16 import org.apache.commons.fileupload.servlet.ServletFileUpload;
17
18 public class FileUploadServlet extends HttpServlet {
19
20 public void doPost(HttpServletRequest request, HttpServletResponse response)
21 throws ServletException, IOException {
22 request.setCharacterEncoding("utf-8");//防止中文名乱码
23 int sizeThreshold=1024*6; //缓存区大小
24 String basePath = this.getServletContext().getRealPath("/upload/");
25 File repository = new File(basePath); //缓存区目录
26 long sizeMax = 1024 * 1024 * 2;//设置文件的大小为2M
27 final String allowExtNames = "jpg,gif,bmp,rar,rar,txt,docx";
28 DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
29 diskFileItemFactory.setRepository(repository);
30 diskFileItemFactory.setSizeThreshold(sizeThreshold);
31 ServletFileUpload servletFileUpload=new ServletFileUpload(diskFileItemFactory);
32 servletFileUpload.setSizeMax(sizeMax);
33
34 List<FileItem> fileItems = null;
35 try{
36 fileItems = servletFileUpload.parseRequest(request);
37
38 for(FileItem fileItem:fileItems){
39 long size=0;
40 String filePath = fileItem.getName();
41 System.out.println(filePath);
42 if(filePath==null || filePath.trim().length()==0)
43 continue;
44 String fileName=filePath.substring(filePath.lastIndexOf(File.separator)+1);
45 // String fileName=String.valueOf(System.currentTimeMillis());
46 String extName=filePath.substring(filePath.lastIndexOf(".")+1);
47 // fileName+="."+extName;
48 if(allowExtNames.indexOf(extName)!=-1)
49 {
50 try {
51 fileItem.write(new File(basePath+File.separator+fileName));
52 } catch (Exception e) {
53 e.printStackTrace();
54 }
55 }
56 else{
57 throw new FileUploadException("file type is not allowed");
58 }
59 }
60 }catch(FileSizeLimitExceededException e){
61 System.out.println("file size is not allowed");
62 }catch(FileUploadException e1){
63 e1.printStackTrace();
64 }
65
66
67 }
68
69 }
70
追问
我要上创到不同目录, 这个上传到upfile1目录
这个上传到upfile2目录
代码怎么改,拜托您了!!
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询