我希望能够在我的JSF2.2 Web应用程序中上传文件,所以我开始使用新<h:inputFile>组件.
我唯一的问题是,如何指定文件将保存在服务器中的位置?我想把它们当成java.io.File实例.这必须在支持bean中实现,但我不清楚如何理解.
我正在开发一个使用http连接下载文件的项目.我在下载过程中显示一个带有进度条状态的水平进度条.我的功能看起来像这样:
.......
try {
InputStream myInput = urlconnect.getInputStream();
BufferedInputStream buffinput = new BufferedInputStream(myInput);
ByteArrayBuffer baf = new ByteArrayBuffer(capacity);
int current = 0;
while((current = buffinput.read()) != -1) {
baf.append((byte) current);
}
File outputfile = new File(createRepertory(app, 0), Filename);
FileOutputStream myOutPut = new FileOutputStream(outputfile);
myOutPut.write(baf.toByteArray());
...
}
Run Code Online (Sandbox Code Playgroud)
我事先知道文件的大小,所以我需要在下载过程中检索大小(在我的while块中).因此,我将能够确定进度条的状态.
progressBarStatus = ((int) downloadFileHttp(url, app) * 100)/sizefile;
Run Code Online (Sandbox Code Playgroud)
long downloadFileHttp(..,..)是我的函数的名称.
我已经尝试使用outputfile.length检索它,但是他的值是"1",也许它是我试图下载的文件数.
有什么方法可以搞清楚吗?
更新1
我没有任何线索可以让我弄清楚这一点.目前我有一个水平进度条,只显示0和100%whitout中间值.我想另一种方法.如果我知道我的wifi速率和文件的大小,我可以确定下载的时间.
我知道我可以检索我的Wifi连接的信息和我要下载的文件的大小.
有人已经有工作或有线程吗?
我有Uri for Image文件.
我使用此代码从Uri获取文件路径:
public String getRealPathFromURI(Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = { MediaStore.Images.Media.DATA };
cursor = mContext.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} catch (Exception e) {
Log.message(e.getMessage());
} finally {
if (cursor != null) {
cursor.close();
}
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
如果我从Gallery应用程序(Android 4.4.2,arm-emu)中选择图像,
uri.getPath = /external/images/media/16 and it work's fine (My file path: /storage/sdcard/Download/0_cf15a_7800a7e5_orig.jpg)
Run Code Online (Sandbox Code Playgroud)
如果我从Documents app(Android 4.4.2,arm-emu)中选择图像,
I have uri.getPath = /document/image:16 and function getRealPathFromURI returns …Run Code Online (Sandbox Code Playgroud) 我正在使用Java-API,它大量使用Autoclosable-Interface,因此使用Java try-with-resources.但是在Java中您可以指定
try (res1, res2, res3...) {
...
}
Run Code Online (Sandbox Code Playgroud)
我们有办法使用多种资源吗?它看起来像众所周知的回调 - 地狱:
val database = Databases.openDatabase(dbFile)
database.use {
database.createResource(ResourceConfiguration.Builder(resPathName, config).build())
val resMgr = database.getResourceManager(ResourceManagerConfiguration.Builder(resPathName).build())
resMgr.use {
val wtx = resMgr.beginNodeWriteTrx()
wtx.use {
wtx.insertSubtreeAsFirstChild(XMLShredder.createStringReader(resFileToStore))
}
}
}
Run Code Online (Sandbox Code Playgroud) 我在以下代码上收到此错误(请注意,这不会发生在我的本地计算机上,仅在我的构建服务器上):
Files.readAllBytes(Paths.get(getClass().getResource("/elasticsearch/segmentsIndex.json").toURI()), Charset.defaultCharset());
Run Code Online (Sandbox Code Playgroud)
例外情况:
Caused by: java.nio.file.FileSystemNotFoundException: null
at com.sun.nio.zipfs.ZipFileSystemProvider.getFileSystem(ZipFileSystemProvider.java:171)
at com.sun.nio.zipfs.ZipFileSystemProvider.getPath(ZipFileSystemProvider.java:157)
at java.nio.file.Paths.get(Paths.java:143)
Run Code Online (Sandbox Code Playgroud)
我尝试通过遵循此解决方案来解决它; 我的代码现在看起来像这样:
URI segmentsIndexURI = getClass().getResource("/elasticsearch/segmentsIndex.json").toURI();
Map<String, String> env = new HashMap<>();
env.put("create", "true");
FileSystem zipfs = FileSystems.newFileSystem(segmentsIndexURI, env); //exception here
Path segmentsIndexPath = Paths.get(segmentsIndexURI);
Run Code Online (Sandbox Code Playgroud)
我收到以下异常:
java.lang.IllegalArgumentException: Path component should be '/'
at sun.nio.fs.UnixFileSystemProvider.checkUri(UnixFileSystemProvider.java:77)
at sun.nio.fs.UnixFileSystemProvider.newFileSystem(UnixFileSystemProvider.java:86)
at java.nio.file.FileSystems.newFileSystem(FileSystems.java:326)
at java.nio.file.FileSystems.newFileSystem(FileSystems.java:276)
Run Code Online (Sandbox Code Playgroud)
似乎没什么用.我该如何构建文件的路径?
我正在解码http数据包.我遇到了一个问题,就是问题.当我得到一个http数据包时,它有一个标题和正文.当transefer编码被分块时我不知道该怎么办?
是否有一个有用的API或类来解除JAVA中的数据?
如果有人,经验丰富的http解码,请告诉我如何做到这一点?
如何在p:fileUpload中将上传的图像作为BLOB插入MySQL中?
@Lob
@Column(name = "photo")
private byte[] photo;
Run Code Online (Sandbox Code Playgroud)
而在XHTML页面,我写这个:
<p:inputText value="#{condidat.condidat.photo}" >
<p:fileUpload fileUploadListener="#{fileUploadController.handleFileUpload}"
allowTypes="*.jpg;*.png;*.gif;" description="Images"/>
</p:inputText>
Run Code Online (Sandbox Code Playgroud)
如何将上传文件的值检索为byte[]?
我想编码并保存Base64中的URL图像.我发现了几个从本地文件进行编码而不是从URL进行编码的示例.有可能这样做吗?
我尝试过类似的东西,但没有成功.任何线索,有帮助吗?感谢您的回答.
public static void main(String[] args) {
String imageUrl = "http://www.avajava.com/images/avajavalogo.jpg";
String destinationFile = "image.jpg";
try {
// Reading a Image file from file system
URL url = new URL(imageUrl);
InputStream is = url.openStream();
FileInputStream imageInFile = new FileInputStream(is.toString());
byte imageData[] = new byte[2048];
imageInFile.read(imageData);
// Converting Image byte array into Base64 String
String imageDataString = encodeImage(imageData);
System.out.println("imageDataString : " + imageDataString);
System.out.println("Image Successfully Manipulated!");
} catch (FileNotFoundException e) {
System.out.println("Image not found" + e);
} catch (IOException ioe) …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用POST文件HttpClient。但是,我无法访问实际的,File但我可以访问其InputStream。有没有办法我仍然可以发布文件?
到目前为止,这是我所做的:
public void sendFile (InputStream instream) {
HttpClient client = new HttpClient();
PostMethod method = new PostMethod("http://localhost:8080/myservice/testupload");
Part[] parts = new Part[] {
//new FilePart("myFile", file.getName(), file)
};
method.setRequestEntity(
new MultipartRequestEntity(parts, method.getParams()));
client.executeMethod(method);
}
Run Code Online (Sandbox Code Playgroud)
如您所见,FilePart需求文件但是我有InputStream。如何将输入流发布为文件?
我阅读了 Java 10 文档java.io.Reader.transferTo(...),它说:
从此读取器读取所有字符,并按照读取顺序将字符写入给定写入器
transferTo中的 方法Reader将非常有用,因为目前将数据从读取器复制到写入器非常冗长。由于我们在现实生活中经常使用InputStream和OutputStream使用它们,是否有类似的方法?