java实现下载文件代码 java实现下载文件代码的方法

Java中文件下载该怎么写代码求高手指导if (upfile.exists()) {
bytes = FileUtils.readFileToByteArray(upfile);
response.setContentType("application/x-download");
String agent = request.getHeader("USER-AGENT");//用户代理
// 防止中文文件名乱码
if (null != agent-1 != agent.indexOf("MSIE")) {
String codedfilename = StringUtils.replace(URLEncoder.encode(fileName, "UTF-8"), "+", "%20");
response.setHeader("Content-Disposition", "attachment;filename=" + codedfilename);
} else if (null != agent-1 != agent.indexOf("Mozilla")) {
String codedfilename = MimeUtility.encodeText(fileName, "UTF-8", "B");
response.setHeader("Content-Disposition", "attachment;filename=" + codedfilename);
} else {
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
}
response.setContentLength(bytes.length);
response.getOutputStream().write(bytes);
}
java中怎么实现下载功能,最好能有代码private File uploadify;
private String uploadifyFileName;
public String uploadFile1() throws Exception {
String extName = "";// 扩展名
String newFileName = "";// 新文件名
String nowTime = df.format(new Date());// 当前时间
String random = "-" + (Math.round(Math.random() * 9000) + 1000);// 随机函数
String path = "uploads/" + nowTime.substring(0, 6) + "/"
+ nowTime.substring(0, 8) + "/";// 保存路径
String savePath = ServletActionContext.getServletContext().getRealPath(
"");
savePath = savePath.replace("\\", "/");
if (!savePath.substring(savePath.length()).equals("/"))
savePath = savePath + "/";
savePath = savePath + path;
// 获取扩展名
if (uploadifyFileName.lastIndexOf(".") = 0) {
extName = uploadifyFileName.substring(uploadifyFileName
.lastIndexOf("."));
}
newFileName = uploadifyFileName.substring(0,
uploadifyFileName.lastIndexOf("."))
+ nowTime.substring(8) + random + extName;
File file = new File(savePath);
if (!file.exists())
file.mkdirs();
uploadify.renameTo(new File(savePath + newFileName));
/*
* HttpServletResponse response = ServletActionContext.getResponse();
* response.setCharacterEncoding("utf-8");
* response.getWriter().print(uploadifyFileName+"上传成功");
*/
String ctx = Struts2Utils.getRequest().getContextPath();
Struts2Utils.renderText(ctx + "/" + path + newFileName);
Wenjdetail detail = new Wenjdetail();
String pt = path + newFileName;
detail.setName(uploadifyFileName);
detail.setUrl(pt);
wenjdetailManager.saveWenjdetail(detail);
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("utf-8");
response.getWriter().print("," + detail.getId());
return null; // 这里不需要页面转向,所以返回空就可以了
}
JAVA文件下载如何实现在http协议下,实现下载一般就两种方法,一个采用cont-type="";此种方法为附件的方式下载;;
另一种较简单,就是你只需要点下载按钮然后跳转到服务器的那个文件路劲就可以了,浏览器自动回进行下载..
Java文件下载怎么实现的下载就很简单了
把你要下载的文件做成超级链接,可以不用任何组件
比如说
下载一个word文档
a href="https://www.04ip.com/post/名称.doc"名称.doc/a
路径你自己写
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URI;
import java.net.URL;
import java.util.Random;
/**
*
* 实现了下载的功能*/
public class SimpleTh {

推荐阅读