我通过URLEncoder.encode方法进行了转码,可以直接将在浏览器中进行访问并能够看到该文件中的内容,但是并不能提示下载,如果我需要下载的文件没有空格,进行转码后,可以会提示下载。这是为什么,就解决
解决方案 »
- 新手学习一段时间JAVA之后的疑问
- struts2文件上传
- ectable设定过滤后,列宽变的超大
- 如何显示从数据库中取出的图片
- 请教DWR2 + SPRING2 + STRUTS2配置问题
- lib文件至少应该包含哪些文件才能使jsp文件正常显示?
- 我想做一个通过浏览器查看到某个客户端的视频画面,该技术是否应该属于b/c架构呢?哪位大侠能够给点思路。谢谢!
- 请问:JavaMail中如何设置邮件的优先等级?
- 那位有jspsmartupload的包啊,能给我一份吗,有上传的javabean也可以,谢谢!我下载的不能上传文件,急啊
- 寻求struts高手讲解。actionform中的set get reset方法及其他 ! up 有分!
- 页面显示ppt 用的是object class方法 请问class=“clsid:”
- Team没有Synchronize with Repository
/**
* 远程下载文件操作
*
* @param postfix
* 文件相应参数字符串
* @throws Exception
*/
public Integer downLoadDoc(final String fileParamStr) throws Exception {
Integer status = 0;
try {
status = AccessController
.doPrivileged(new PrivilegedAction<Integer>() {
public Integer run() {
// 0 下载成功 -1下载失败 -2 服务器端不存在该文件 -3文件信息写入失败
int stat = 0;
HttpClient httpClient = new HttpClient();
// 创建GET方法的实例
try {
downLoadLocalPath = createFile.basePath;
System.out.println("downPath=="
+ downLoadLocalPath);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String[] strs = fileParamStr.split(";");
String id = null, version = null;
for (int i = 0; i <= strs.length; i++) {
id = strs[0];
version = strs[1];
// mime = strs[2];
// digest = strs[3];
}remoteFileUrl = Constant.REMOTE_SERVER;
String url = remoteFileUrl + id + "/" + version;System.out.println("url=" + url);
GetMethod getMethod = new GetMethod(url);
getMethod.getParams().setParameter(
HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler());
try {
// 执行getMethod
int statusCode = httpClient
.executeMethod(getMethod);
if (statusCode == HttpStatus.SC_OK) {
byte[] responseBody = getMethod
.getResponseBody();
serverfile = downLoadLocalPath
+ File.separator + id + "_"
+ version + ".txt";
OutputStream serverout = new FileOutputStream(
serverfile);
serverout.write(responseBody);
serverout.flush();
serverout.close();
System.out.println("文件下载结束,将文件信息写入XML配置文件");
WikisBean wikiBean = new WikisBean();
// 从服务器获取下载文件信息
try {
wiki = xml.findElement(
Constant.REMOTE_DATAS, id,
version);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
wikiBean.setId(id);
if (wiki != null) {
wikiBean.setItemId(wiki.getItemId());
wikiBean.setDigest(wiki.getDigest());
wikiBean.setMime(wiki.getMime());
} else {wikiBean.setItemId("");
wikiBean.setDigest("");
wikiBean.setMime("");
}
wikiBean.setVer(version);
wikiBean.setPath(serverfile);
// 将文件信息写入XML配置文件
try {
xml.writeOrCreate(
createFile.repositoryPath,
wikiBean);
} catch (Exception e) {
// TODO Auto-generated catch block
stat = -3;
e.printStackTrace();}
// 打开该文件
selFile.openFileByPath(serverfile);
System.out.println("文件打开成功");} else {System.err.println("Method failed: "
+ getMethod.getStatusLine());
}// 读取内容} catch (HttpException e) {
// 发生致命的异常,可能是协议不对或者返回的内容有问题
System.out
.println("Please check your provided http address!");
e.printStackTrace();
stat = -1;
} catch (IOException e) {
// 发生网络异常
e.printStackTrace();
stat = -1;
} finally {
// 释放连接
getMethod.releaseConnection();
}return stat;
}
});
} catch (Throwable t) {
if (t instanceof PrivilegedActionException) {
PrivilegedActionException pe = (PrivilegedActionException) t;
t = pe.getException() == null ? pe : pe.getException();
}
System.out.println(t.toString());
}
System.out.println(status);
return status;
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String sufix = request.getParameter("s");
String path = request.getParameter("p") + "." + sufix;
try {
String fileName = request.getParameter("f").trim();
int c = fileName.lastIndexOf(".");
String name = fileName.substring(0, c > 0 ? c : fileName.length())
+ "." + sufix;
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachement;filename="
+ new String(name.getBytes("GBK"), "ISO-8859-1"));
File file = new File(Const.getCurrentUtterlyPath() + path);
if (!file.exists()) {
throw new IOException(fileName + ",所下载的文件不存在!");
}
response.setContentLength(Integer.parseInt(file.length() + ""));
InputStream fs = new FileInputStream(file);
OutputStream os = response.getOutputStream();
byte[] buff = new byte[1024];
int readCount = 0;
while ((readCount = fs.read(buff)) != -1) {
os.write(buff, 0, readCount);
}
if (fs != null) {
fs.close();
}
if (os != null) {
os.close();
}
} catch (IOException e) {
LOG.error("error: " + e.getMessage() + ",path: " + path);
throw e;
}
response.setStatus(response.SC_OK);
response.flushBuffer();
}