我们在Java编程中往往都会使用文件操作,可是在不同的平台上,文件的路径总是一个让人头疼的小问题,比如在win7平台上,非管理员对系统盘无使用权限等等。可以使用
写道
try {
InputStream is = attachment.getActivationDataHandler()
.getInputStream();
// byte[] buffer = new byte[4096];
// while((flag = is.read(buffer))!=-1){
// fos.write(buffer,0,flag);
// }
//系统临时文件夹路径
String tempoutpath = System.getProperty("java.io.tmpdir");
Date date = new Date();
File tmpFile = new File(tempoutpath,"temp_pic_" + date.getTime()
+ ".jpg");
System.out.println("临时文件夹:"+tmpFile.getAbsolutePath());
filePathArray[filePathArrayIndex] = tmpFile.getAbsolutePath();
FileOutputStream fos = new FileOutputStream(tmpFile);
ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
int ch;
while ((ch = is.read()) != -1) {
bytestream.write(ch);
}
byte imgdata[] = bytestream.toByteArray();
bytestream.close();
fos.write(imgdata);
is.close();
fos.close();
imagefiles.add(imgdata);
} catch (IOException e) {
e.printStackTrace();
}
InputStream is = attachment.getActivationDataHandler()
.getInputStream();
// byte[] buffer = new byte[4096];
// while((flag = is.read(buffer))!=-1){
// fos.write(buffer,0,flag);
// }
//系统临时文件夹路径
String tempoutpath = System.getProperty("java.io.tmpdir");
Date date = new Date();
File tmpFile = new File(tempoutpath,"temp_pic_" + date.getTime()
+ ".jpg");
System.out.println("临时文件夹:"+tmpFile.getAbsolutePath());
filePathArray[filePathArrayIndex] = tmpFile.getAbsolutePath();
FileOutputStream fos = new FileOutputStream(tmpFile);
ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
int ch;
while ((ch = is.read()) != -1) {
bytestream.write(ch);
}
byte imgdata[] = bytestream.toByteArray();
bytestream.close();
fos.write(imgdata);
is.close();
fos.close();
imagefiles.add(imgdata);
} catch (IOException e) {
e.printStackTrace();
}
将文件存储到本登录用户的临时文件存储文件夹下。
String tempoutpath = System.getProperty("java.io.tmpdir");
是获取当前用户的临时文件存储文件夹路径。利用Java的系统环境会获得的。
比如win7下获取的就是C:\Users\liuyan\AppData\Local\Temp。