2011-12-12 1 views

ответ

1

Рассматривая документацию (UploadedFile и File) для обоих классов, вот одно решение.

Поскольку вы можете получить доступ к InputStreamUploadedFile, вы можете прочитать данные загруженного файла и записать его во временное местоположение или в другое место, в котором может работать ваше приложение.

// assume that you have the UploadedFile object named uploadedFile 
InputStreamReader reader = new InputStreamReader(uploadedFile.getInputStream()); 
int partition = 1024; 
int length = 0; 
int position = 0; 
char[] buffer = new char[partition]; 
FileWriter fstream = new FileWriter("out.tmp"); 
do{ 
    length = reader.read(buffer, position, partition) 
    fstream.write(buffer, position, length); 
}while(length > 0); 
File file = new File("out.tmp"); 
+0

ошибка, указанная в следующей строке- ** while (length = reader.read (buffer, position, partition)) ** Тип несоответствия: невозможно преобразовать из int в boolean – JohnC

+0

исправлено. должен работать здесь. – mauris

+0

Спасибо за помощь. – JohnC

4

Самый простой способ заключается в использовании Apache Commons FileUtils.

File destFile= new File("somefile.txt"); 
FileUtils.copyInputStreamToFile(uploadedFile.getInputStream(), destFile); 
0
InputStreamReader reader = new InputStreamReader(uploadedFile.getInputstream()); 
BufferedReader br = new BufferedReader(reader); 
File f = new File("file.txt"); 
FileWriter fw = new FileWriter(f,false); 
BufferedWriter bw = new BufferedWriter(fw); 

while((line = br.readLine()) != null){ 
    fw.write(line + System.lineSeparator()); 
} 

Этот код будет вернуть загруженный файл в текстовом файле.

Смежные вопросы