-1
A
答えて
4
ここでは動作例です。 InputStreamを使用してInputStreamを変更する必要があります。また、作業/ tmpディレクトリ()の場所を変更することもできます。
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItem;
public class TestFile {
public static void main(String args[]) throws IOException {
// This is a sample inputStream, use your own.
InputStream inputStream = new FileInputStream("c:\\Kit\\Apache\\geronimo-tomcat6-javaee5-2.1.6\\README.txt");
int availableBytes = inputStream.available();
// Write the inputStream to a FileItem
File outFile = new File("c:\\tmp\\newfile.xml"); // This is your tmp file, the code stores the file here in order to avoid storing it in memory
FileItem fileItem = new DiskFileItem("fileUpload", "plain/text", false, "sometext.txt", availableBytes, outFile); // You link FileItem to the tmp outFile
OutputStream outputStream = fileItem.getOutputStream(); // Last step is to get FileItem's output stream, and write your inputStream in it. This is the way to write to your FileItem.
int read = 0;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
// Don't forget to release all the resources when you're done with them, or you may encounter memory/resource leaks.
inputStream.close();
outputStream.flush(); // This actually causes the bytes to be written.
outputStream.close();
// NOTE: You may also want to delete your outFile if you are done with it and dont want to take space on disk.
}
}
関連する問題
- 1. ファイルディレクトリをJavaでInputStreamのリストに変換する方法8
- 2. オブジェクトをInputstreamに変換するには
- 3. バイト配列をJavaのInputStreamに変換できますか?
- 4. InputStreamReaderをInputStreamに変換する
- 5. Javaバイト[]からFileItemへ
- 6. InputStreamデータを別のデータ型に変換する
- 7. ファイル/ Inputstreamへのコレクションの変換
- 8. Inputstream java
- 9. ファイルを変換します.InputstreamをUTF-8に変換します。C#
- 10. JavaのInputStream
- 11. Java InputStreamのサイズ
- 12. InputStreamをSourceに変換する方法は?
- 13. SAX ContentHandler文字列(..)をInputStreamに変換する
- 14. UTF文字列をInputStreamに変換する
- 15. Java Process InputStreamのバグ?
- 16. InputStreamを指定してバイトシーケンスを変換する
- 17. Java/GroovyでInputStreamをBufferedImageに変換するにはどうすればよいですか?
- 18. Java MEでStringBufferをInputStreamに変換するにはどうすればよいですか?
- 19. オブジェクトをJavaに変換するベクトルをベクトルに変換する
- 20. InputStreamのLatin-1の内容をUTF-8文字列に変換する
- 21. Java SQL Result to InputStream
- 22. のJava:のInputStreamマーク限度
- 23. InputStream - ネットワークの変更を処理する
- 24. StringをJavaのClobに変換する
- 25. リストをJavaのjsonに変換する
- 26. C++のビットパターンをJavaに変換する
- 27. ArrayをJavaのJsonに変換する
- 28. wsdlをjavaに変換するには?
- 29. JavaのInputstreamの終了を取得するには
- 30. Java InputStreamはデータを待機します。
どのAPIのFileItemですか? – Vulcan
org.apache.commons.fileupload – MrGreen