我正在尝试使用Extjs
and上传多个文件,Java
并且我正在使用“application/x-www-form-urlencoded”内容类型并从中获取所有数据和文件的名称HttpRequest
。我需要上传可能的文件。它可以是文档类型或图像类型。
我的问题如下:
1)在提供静态文件路径的同时,该静态位置的所有文件都将成功上传。但是我需要从不同的位置而不是一个静态位置上传多个文件,所以请尽快帮助我..
这是我的文件上传控制器代码
@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 for 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 and 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);
for (int i = 0; i < b.length; i++) {
b[i] = dis.readByte();
}
dos.write(b);
}
} catch (IOException e) {
System.out.println("IOException" + e.getMessage());
e.printStackTrace();
return success;
} finally {
if (dos != null) {
closeOutputStream(dos);
}
if (fos != null) {
closeOutputStream(fos);
}
}
return success;
}