java - Multiple files upload -
i trying upload multiple files using extjs
, java
, using " application/x-www-form-urlencoded " content type , data , files' names httprequest
. , need upload may kind of file. can doc type or image type.
problem following:
1) while giving static file path files static location uploaded successfully. need upload multiple files different locations rather 1 static location, please me possible..
this file upload controller code
@requestmapping(value = "views/upload.action", method = requestmethod.post) public @responsebody map<string, object> create(httpservletrequest request) throws ioexception { map<string, object> fileupload = new hashmap(); boolean success = false; enumeration headernames = request.getheadernames(); while (headernames.hasmoreelements()) { string headername = (string) headernames.nextelement(); if (headername.equals("x-file-name")) { system.out.println("header " + headername); system.out.println(request.getheader(headername)); bufferedreader reader = request.getreader(); stringbuilder sb = new stringbuilder(); string line = reader.readline(); while (line != null) { sb.append(line + "\n"); line = reader.readline(); } reader.close(); string data = sb.tostring(); success = copyfile(data, request.getheader(headername)); } } fileupload.put("success", success); return fileupload; } // open file input private boolean openfileinput(file file) { try { fis = new fileinputstream(file); dis = new datainputstream(new bufferedinputstream(fis)); return true; } catch (filenotfoundexception e) { e.printstacktrace(); logger.error("file not found...!"); return false; } } // copies file private boolean copyfile(string filedata, string filename) { file file = new file("c://documents , settings/infosoft03/desktop/" + filename); //dis = new datainputstream(new bufferedinputstream(inputstream)); // file file = new file(filename); boolean success = false; try { file dir = new file(propertyreader.getvalue("uploadpath")); if (!dir.exists()) { dir.mkdir(); } // org.apache.commons.io.fileutils.writestringtofile(file, // filedata); //success = openfileinput(file); if (success) { success = true; fos = new fileoutputstream( propertyreader.getvalue("uploadpath") + filename); dos = new dataoutputstream(new bufferedoutputstream(fos)); byte[] b = new byte[(int) file.length()]; system.out.println("file length" + b.length); (int = 0; < b.length; i++) { b[i] = dis.readbyte(); } dos.write(b); } } catch (ioexception e) { system.out.println("ioexception" + e.getmessage()); e.printstacktrace(); return success; } { if (dos != null) { closeoutputstream(dos); } if (fos != null) { closeoutputstream(fos); } } return success; }
Comments
Post a Comment