急!用javabean实现下载功能的后下载的文件变乱码 你是用什么写的JSP Or Servlet?如果你是用JSP,那么你么注意,在<%@%>这些所有的标记之间是不能存在回车的,你么把它们都删掉 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我这个下了一个exe,可以运行,下了一个txt,里面的gb2312码没变,清清楚楚啊。package test;import javax.servlet.*;import javax.servlet.http.*;import java.io.*;import java.util.*;public class Servlet1 extends HttpServlet { private static final String CONTENT_TYPE = "text/html; charset=GBK"; //Initialize global variables public void init() throws ServletException { } //Process the HTTP Get request public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.getFile("D:\\文档1234567890.txt", request, response); } public static void getFile(String filename, HttpServletRequest request, HttpServletResponse response) throws IOException { long totalsize = 0; File f = new File(filename); long filelength = f.length(); byte[] b = new byte[(int)filelength]; FileInputStream fin = new FileInputStream(f); DataInputStream in = new DataInputStream(fin); in.read(b); response.setContentType("application/exe"); filename = filename.substring(filename.lastIndexOf("\\") + 1, filename.length()); response.setHeader("Content-Disposition", "attachment;filename="+filename); String filesize = "" + filelength; response.setHeader("Content-Length", filesize); ServletOutputStream servletOut = response.getOutputStream(); servletOut.write(b); servletOut.flush(); servletOut.close(); } //Clean up resources public void destroy() { }} To ChDw(米),不是你说的那些问题,呵呵,不过还是谢谢了!我用的是jbuilder,我测试用的jsp2.jsp文件代码如下:<%@ page import="test.file" contentType="text/html; charset=gb2312"%><% String strfile = "D:\\123.txt"; file.getfile1(strfile, request, response);%>123.txt存在服务器的d盘上,浏览jsp2.jsp下载文件后用winHex比较源文件和下载后的文件,发现首尾个多了一个Hex(0A),以至出现乱码,翻遍所有的书和jbuilder自带的帮助均没找到说法,还请各位大虾赐教!!! 执行Runtime.getRuntime().exec()时空间不足 大神们 ,这个错误怎么解? 应该很简单的!小弟急啊 请教一个简单的问题 如何获取一个程序执行后的时间和内存? 求thinking in java 4练习题的答案 加和(相乘)问题,请教算法达人,帮我解决这一困扰我许久的问题。 渔夫分鱼算法 求解 100分请教BufferedReader类的问题。 呵呵,我也收到IBM的光盘了 关于RMI的一个问题 Mysql数据库连结问题,紧急,紧急,求救!!! 请问这是什么原因???谢谢了
import javax.servlet.http.*;
import java.io.*;
import java.util.*;public class Servlet1 extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.getFile("D:\\文档1234567890.txt", request, response);
} public static void getFile(String filename, HttpServletRequest request, HttpServletResponse response) throws IOException
{
long totalsize = 0;
File f = new File(filename);
long filelength = f.length();
byte[] b = new byte[(int)filelength];
FileInputStream fin = new FileInputStream(f);
DataInputStream in = new DataInputStream(fin);
in.read(b);
response.setContentType("application/exe");
filename = filename.substring(filename.lastIndexOf("\\") + 1, filename.length());
response.setHeader("Content-Disposition", "attachment;filename="+filename);
String filesize = "" + filelength;
response.setHeader("Content-Length", filesize);
ServletOutputStream servletOut = response.getOutputStream();
servletOut.write(b);
servletOut.flush();
servletOut.close();
} //Clean up resources
public void destroy() {
}
}
我用的是jbuilder,我测试用的jsp2.jsp文件代码如下:
<%@ page import="test.file" contentType="text/html; charset=gb2312"%>
<%
String strfile = "D:\\123.txt";
file.getfile1(strfile, request, response);
%>
123.txt存在服务器的d盘上,浏览jsp2.jsp下载文件后用winHex比较源文件和下载后的文件,发现首尾个多了一个Hex(0A),以至出现乱码,翻遍所有的书和jbuilder自带的帮助均没找到说法,还请各位大虾赐教!!!