加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

文件下载,带转码->pdf->swf

发布时间:2020-12-15 20:09:24 所属栏目:百科 来源:网络整理
导读:private String upload = "保存的路径"; //文件下载 public String download() { //初始化 this.initContext(); String apath = request.getSession().getServletContext().getRealPath(upload); File af = new File(apath); // 如果没有这个文件夹,就创建

private String upload = "保存的路径";


//文件下载

public String download() {
//初始化
this.initContext();
String apath = request.getSession().getServletContext().getRealPath(upload);

File af = new File(apath);
// 如果没有这个文件夹,就创建个文件夹
if (!af.exists()) {
af.mkdirs();
}


String filenewaddress = request.getSession().getServletContext().getRealPath(upload);
? ??
System.out.println("开始下载,请稍后……");
URL urlfile = null;
HttpURLConnection httpUrl = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {

// 把下载地址赋给urlfile?
urlfile = new URL(fileoldaddress);
// 打开下载 URL 引用的资源的通信链接(如果尚未建立这样的连接)
httpUrl = (HttpURLConnection) urlfile.openConnection();
// 建立与指定socket的连接
httpUrl.connect();
// 提供一个默认的文件名
String context = httpUrl.getHeaderField("Content-Disposition");
// 判断context是否为空
if (context == null) {
// 取地址最后的/后面的字符当做名字
filename1 = fileoldaddress.substring(fileoldaddress
.lastIndexOf("/") + 1,fileoldaddress.length());
filenewaddress = filenewaddress + "" + filename1;
} else {
filename1 = context.substring(context.lastIndexOf("=") + 1,
context.length());
filenewaddress = filenewaddress + "" + filename1;
}
// 把本地保存地址赋给f
File f = new File(filenewaddress);
bis = new BufferedInputStream(httpUrl.getInputStream());
bos = new BufferedOutputStream(new FileOutputStream(f));
int len = 2048;
byte[] b = new byte[len];
while ((len = bis.read(b)) != -1) {
bos.write(b,len);
}
bos.flush();
bis.close();
httpUrl.disconnect();
System.out.println("下载完成!");
} catch (Exception e) {
System.out.println("无效下载地址,错误信息:" + e.getMessage());
this.msg = "下载失败!";
this.success = false;
} finally {
try {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
} catch (IOException e) {
System.out.println("系统错误,错误信息:" + e.getMessage());
System.out.println("请联系管理员!");
}
}


// 下载到本地结束

// 取绝对路径+名
String filepath = apath + "" + filename1;


System.out.println(filepath);
// 转格式
DocConverter d = new DocConverter(filepath);
d.conver();
// 转格式结束
return "success";

}


转码需要用到两个软件! ? ? Apache_OpenOffice_incubating_3.4.1_Win_x86_install_zh-CN

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??swftools

必须先启动这两个软件,才能转码

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读