JAVA版-SWFUpload使用(包括传参问题)(二)

2014-11-24 08:32:02 · 作者: · 浏览: 1
leList");
11
serdata.innerHTML = serverData;
12

13
} catch (ex) {
14
this.debug(ex);
15
}
16
}
三、后台servlet-Upload.java,下边只列出了用到的doPost()方法,其他内容都没有变化

01
public void doPost(HttpServletRequest request, HttpServletResponse response)
02
throws ServletException, IOException {
03

04
//接收参数
05
String hello = request.getParameter("hello");
06
String name = request.getParameter("name");
07
name = new String(name.getBytes("ISO-8859-1"),"UTF-8");//字符编码问题,可以通过前台encoding后台再解析
08

09
System.out.println("接收参数,hello="+hello+",name="+name);
10

11
String path1 = request.getRequestURI()+"/files";
12
System.out.println("____________request.getRequestURI():"+path1);
13
String path2 = request.getSession().getServletContext().getRealPath("/");
14
System.out.println("_________path2:"+path2);
15

16
List fileList;
17
fileList = (List)request.getAttribute("fileList");
18
if(fileList==null)
19
fileList = new ArrayList();
20

21
//接收上传文件
22
String uploadSign = request.getParameter("upload");
23
String rootPath = request.getParameter("rootPath");
24
String path = request.getParameter("path");
25
if(rootPath == null) rootPath = "";
26
rootPath = rootPath.trim();
27
if(rootPath.equals("")){
28

//rootPath = application.getRealPath("/swfupload/files");//自由修改处二:指定服务器固定文件
29
rootPath = path2+"/files";
30
}
31

32
if(path == null) {
33
path = rootPath;
34
}else{
35
path = new String(Base64.decodeBase64(path.getBytes()));
36
}
37
System.out.println(path+"...path.getBytes():"+path.getBytes());
38
uploadSign = "1";
39
//上传操作
40
if(null != uploadSign && !"".equals(uploadSign)){
41
FileItemFactory factory = new DiskFileItemFactory();
42
ServletFileUpload upload = new ServletFileUpload(factory);
43
//upload.setHeaderEncoding("UTF-8");
44
try{
45
List items = upload.parseRequest(request);
46
if(null != items){
47
Iterator itr = items.iterator();
48
int i = 0;
49
while(itr.hasNext()){
50
FileItem item = (FileItem)itr.next();
51
FileEntity file = new FileEntity();//_____________________
52
if(item.isFormField()){
53
continue;
54
}else{
55
//自由修改处三:可修改上传后的文件命名方式
56
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddkkmmss");//以当前精确到秒的日期为上传的文件的文件名
57
SimpleDateFormat sdd=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
58
String type = item.getName().split("\\.")[1];//获取文件类型
59

60
System.out.println("——————————————————文件名称:"+item.getName());
61
System.out.println("从GBK转到UTF-8输出:"+new String(item.getName().getBytes("GBK"),"UTF-8"));