app中下载的文件怎么存储到手机中,目前我从app中发送请求到后台并能下载文件,但是不知道怎么存储;谢谢各位大神啦
看你用的什么网络框架了,基本都是存在你设置的目录的里面
app查看文件后就储存在手机里了,可通过连接电脑通过手机管理软件在app文件目录里查找下载文件。
public String downFiletxt(@RequestParam("downfilename")String filename,String oldname,HttpServletRequest request,HttpServletResponse response) throws IOException{
//String path = request.getSession().getServletContext().getRealPath("/");
ResourceBundle rb = ResourceBundle.getBundle("server");
String urlStr = rb.getString("upload.url");
String filePath = "F://xq_down";//这个是我测试能否下载都电脑中的地址,亲测ok,但是我想存在手机中怎么办??
File file=new File(filePath);
//判断文件夹是否存在
if (!file.exists())
{
//如果文件夹不存在,则创建新的的文件夹
file.mkdirs();
}
FileOutputStream fileOut = null;
HttpURLConnection conn = null;
InputStream inputStream = null;
try
{
// 建立链接
URL httpUrl=new URL(urlStr+filename);
conn=(HttpURLConnection) httpUrl.openConnection();
//以Post方式提交表单,默认get方式
//conn.setRequestMethod(method);
conn.setDoInput(true);
conn.setDoOutput(true);
// post方式不能使用缓存
conn.setUseCaches(false);
//连接指定的资源
conn.connect();
//获取网络输入流
inputStream=conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(inputStream);
//判断文件的保存路径后面是否以/结尾
if (!filePath.endsWith("/")) {
filePath += "/";
}
//写入到文件(注意文件保存路径的后面一定要加上文件的名称)
fileOut = new FileOutputStream(filePath+oldname);
BufferedOutputStream bos = new BufferedOutputStream(fileOut);
byte[] buf = new byte[4096];
int length = bis.read(buf);
//保存文件
while(length != -1)
{
bos.write(buf, 0, length);
length = bis.read(buf);
}
bos.close();
bis.close();
conn.disconnect();
} catch (Exception e)
{
e.printStackTrace();
System.out.println("抛出异常!!");
}
return null;
如果我使用h5+的plus.downloader.createDownload( url, options, completedCB );的这个方法来下载文件,但是文件路径不会设置,我想存储在手机中的download文件夹下面,请问该如何是好???
一样的 把你的路径修改一下 Environment.getExternalStorageDirectory()+文件名 判断一下文件夹是否存在 如果有写入 如果没有创建 android 5.0以上的手机写入之前最好先获取一下读写权限 要不然你可能写不进去哦
读写权限获取
String[] PERMISSIONS_STORAGE = {
android.Manifest.permission.READ_EXTERNAL_STORAGE,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE
};
int permission = ActivityCompat.checkSelfPermission(AppManager.getAppManager().currentActivity(), android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
AppManager.getAppManager().currentActivity(),
PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE
);
}