В моем проекте я загружаю файл. При загрузке я сохраняю исходное имя файла и расширение в базе данных и сохраняю этот файл с некоторым GUID
на сервере, сгенерированный GUID также сохраняется в базе данных вместе с именем файла и расширением.
Например,
- Имя файла для загрузки - questions.docx
-Тогда orignalFileName будет "вопросами"
-FileExtension будет ".docx"
-Файл будет загружен с именем файла как "0c1b96d3-af54-40d1-814d-b863b7528b1c"
Загрузка работает нормально... но когда я загружаю какой-то файл, он загружается с именем файла как GUID в вышеприведенном случае, его "0c1b96d3-af54-40d1-814d-b863b7528b1c".
Как загрузить файл с его исходным именем файла, т.е. "questions.docx".
Добавлен код
/**
* code to display files on browser
*/
File file = null;
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
try {
/**
* C://DocumentLibrary// path of evidence library
*/
String fileName = URLEncoder.encode(fileRepo.getRname(), "UTF-8");
fileName = URLDecoder.decode(fileName, "ISO8859_1");
response.setContentType("application/x-msdownload");
response.setHeader("Content-disposition", "attachment; filename="+ fileName);
String newfilepath = "C://DocumentLibrary//" + systemFileName;
file = new File(newfilepath);
fis = new FileInputStream(file);
bos = new ByteArrayOutputStream();
int readNum;
byte[] buf = new byte[1024];
try {
for (; (readNum = fis.read(buf)) != -1;) {
bos.write(buf, 0, readNum);
}
} catch (IOException ex) {
}
ServletOutputStream out = response.getOutputStream();
bos.writeTo(out);
} catch (Exception e) {
// TODO: handle exception
} finally {
if (file != null) {
file = null;
}
if (fis != null) {
fis.close();
}
if (bos.size() <= 0) {
bos.flush();
bos.close();
}
}
Является ли этот код идеальным?