急,关于文件处理问题

licdut 2004-10-28 08:08:02
web服务器上有各种类型文件,其路径和扩展名都存在数据库中.现在需提供一个打开这些文件的功能.对于文本文件,在IE中打开即可,对于非文本文件则提供下载的功能.我先用readLine的方式读取文件,文本文件正常,二进制文件如图片不能正常下载,改用InputStream的方法,则文本文件和图片文件都是乱码,请问这是什么原因?
另外象我这种情况,还有没有更好的解决办法?不要使用直接加虚拟路径的方式
...全文
74 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
gks_cn 2004-10-29
  • 打赏
  • 举报
回复
package util.db;
import javax.servlet.*;
import javax.servlet.http.*;

import office.file.File;
import oracle.sql.BLOB;
import sun.jdbc.rowset.CachedRowSet;
import util.string.StringUtil;
import java.io.*;
import java.util.*;
import java.sql.*;
public class DownloadFile extends HttpServlet {
//Initialize global variables
public void init() throws ServletException {}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String contentType = request.getParameter("contentType");
if (contentType == null) {
contentType = "text/html; charset=GBK"; //默认情况下输出的类型为中文文本
}
String uploadId = request.getParameter("uploadId");
if (uploadId == null) {
uploadId = "";
}
String fileSerial = request.getParameter("fileSerial");
if(fileSerial==null) fileSerial = "";
String fileName = "FILE"+fileSerial;

File file = new File();
file.setFileSerial(fileSerial);
file.loadRecord();

String fileSuffix = file.getFileFormat();

Connection conn = null;
Statement ps = null;
ResultSet rs = null;
OpenDbBean db = new OpenDbBean();
try {
response.setContentType(contentType);
ServletOutputStream out = response.getOutputStream();
String sql =
"select * from uploadfiles where filename='" + fileName + "'";
System.out.println(sql);
conn = db.getConnection();
ps = conn.createStatement();
rs = ps.executeQuery(sql);
if (rs.next()) {
BLOB blob = (BLOB) rs.getBlob("binaryfile");
//发送文件名称和文件后缀
String head = fileName + "." + fileSuffix;
System.out.println(head);
response.setHeader(
"Content-disposition",
"attachment;filename=" + head);
int blobsize = (int) blob.length();
InputStream pi = blob.getBinaryStream();
byte[] blobbytes = new byte[blobsize];
int bytesRead = 0;
while ((bytesRead = pi.read(blobbytes)) != -1) {
out.write(blobbytes, 0, bytesRead);
}
pi.close();
out.flush();
} else {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
}
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
try {
db.CleanConnection(conn, ps, rs);
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}
//Clean up resources
public void destroy() {}
}
luckydog903 2004-10-29
  • 打赏
  • 举报
回复
支持加学习
jinannanhai79 2004-10-29
  • 打赏
  • 举报
回复
<% // 得到文件名字和路径
String filename = "GoldWave.rar";
String filepath = "d:\\GoldWave\\";
// 设置响应头和下载保存的文件名
response.reset();
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition","attachment; filename= \"" + filename + "\"");

// 打开指定文件的流信息
java.io.FileInputStream fileInputStream =new java.io.FileInputStream(filepath + filename);
// 写出流信息
int i;
while ((i=fileInputStream.read()) != -1) {
out.write(i);
}
fileInputStream.close();
out.close();
%>
jinannanhai79 2004-10-29
  • 打赏
  • 举报
回复
将其路径转换为绝对路径,然后下载

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧