JSP 文件上传体会(采用 JAVABEAN 上传) 从网上当了一个例子,发觉上传图片文件时图片文件中内容发生了变化,最大的变化时所有的 FF 变成了 3F 也就是说输出时把字符当成有符号传送了?(是这样把?) 使用代码如下: PrintWriter pw = new" name="description" />
MILY: Verdana">JSP文件上传体会(采用JAVABEAN上传) 从网上当了一个例子,发觉上传图片文件时图片文件中内容发生了变化,最大的变化时所有的FF变成了3F 也就是说输出时把字符当成有符号传送了?(是这样把?) 使用代码如下: PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter((savePath==null? "" : savePath) + filename))); while (i != -1 && !newLine.startsWith(boundary)) { // 文件内容的最后一行包含换行字符 // 因此我们必须检查当前行是否是最 // 后一行 i = in.readLine(line, 0, 1280); if ((i==boundaryLength+2 || i==boundaryLength+4) && (new String(line, 0, i).startsWith(boundary))) pw.print(newLine.substring(0, newLine.length()-2)); else pw.print(newLine); newLine = new String(line, 0, i); } pw.close(); 但是总是不行,后来我把PrintWriter用FileOutputStream替代,代码如下: FileOutputStream pw=new FileOutputStream((savePath==null? "" : savePath) + filename); //PrintWriter pw = new PrintWriter(new BufferedWriter(new //FileWriter((savePath==null? "" : savePath) + filename))); while (i != -1 && !newLine.startsWith(boundary)) { // 文件内容的最后一行包含换行字符 // 因此我们必须检查当前行是否是最 // 后一行 i = in.readLine(line, 0, 1280); if ((i==boundaryLength+2 || i==boundaryLength+4) && (new String(line, 0, i).startsWith(boundary))) pw.write(newLine.substring(0, newLine.length()-2).getBytes("ISO8859_1")); else pw.write(newLine.getBytes("ISO8859_1")); newLine = new String(line, 0, i,"ISO8859_1"); } pw.close(); 竟然就可以了,那个转换的编码还一定要上才行,不然也不干活的,真是郁闷,这跨平台就不能让写CODE的人解放解放,一个输出都N多类,看着心寒,想着简直想死啊 哎 忙了我一个下午,原来就是这回事 下面把原代码附上,是把别人的代码改吧改吧放上来的,还请原作者不要见意: Java bean 文件: package cr.web; import javax.servlet.http.HttpServletRequest; import javax.servlet.ServletInputStream; import java.util.Dictionary; import java.util.Hashtable; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.io.*; public class FileUploadBean { private String savePath, filepath, filename, contentType; private byte[] b; byte t; private Dictionary fields; public String getFilename() { return filename; } public String getFilepath() { return filepath; } public void setSavePath(String savePath) { this.savePath = savePath; } public String getContentType() { return contentType; } public String getFieldValue(String fieldName) { if (fields == null || fieldName == null) return null; return (String) fields.get(fieldName); } private void setFilename(String s) { if (s==null) return; int pos = s.indexOf("filename=\""); if (pos != -1) { filepath = s.substring(pos+10, s.length()-1); // Windows浏览器发送完整的文件路径和名字 pos = filepath.lastIndexOf("\\"); if (pos != -1) filename = filepath.substring(pos + 1); else filename = filepath; } } private void setContentType(String s) { if (s==null) return; int pos = s.indexOf(": "); if (pos != -1) contentType = s.substring(pos+2, s.length()); } public void getByte(HttpServletRequest request) { DataInputStream is; int i=0; try { is=new DataInputStream(request.getInputStream()); b=new byte[request.getContentLength()]; while (true) { try { t=is.readByte(); b[i]=t; i++; } catch(EOFException e) { break;} } is.close();} catch(IOException e) {} } public void doUpload1(HttpServletRequest request) throws IOException { byte[] line=new byte[128]; FileOutputStream os=new FileOutputStream("c:\\Demo.out"); ServletInputStream in = request.getInputStream(); getByte(request); String temp=""; temp=new String(b,"ISO8859_1"); byte[] img=temp.getBytes("ISO8859_1"); for (int i=0;i<img.length;i++) { os.write(img[i]); } os.close(); } public void doUpload(HttpServletRequest request) throws IOException { request.setCharacterEncoding("GB2312"); ServletInputStream in = request.getInputStream(); byte[] line = new byte[1280]; int i = in.readLine(line, 0, 1280); if (i < 3) return; int boundaryLength = i - 2; String boundary = new String(line, 0, boundaryLength); //-2丢弃换行字符 fields = new Hashtable(); while (i != -1) { String newLine = new String(line, 0, i); if (newLine.startsWith("Content-Disposition: form-data; name=\"")) { if (newLine.indexOf("filename=\"") != -1) { setFilename(new String(line, 0, i-2)); if (filename==null) return; //文件内容 i = in.readLine(line, 0, 1280); setContentType(new String(line, 0, i-2)); i = in.readLine(line, 0, 1280); //空行 i = in.readLine(line, 0, 1280); newLine = new String(line, 0, i,"ISO8859_1"); FileOutputStream pw=new FileOutputStream((savePath==null? "" : savePath) + filename); //PrintWriter pw = new PrintWriter(new BufferedWriter(new //FileWriter((savePath==null? "" : savePath) + filename))); while (i != -1 && !newLine.startsWith(boundary)) { // 文件内容的最后一行包含换行字符 // 因此我们必须检查当前行是否是最 // 后一行 i = in.readLine(line, 0, 1280); if ((i==boundaryLength+2 || i==boundaryLength+4) && (new String(line, 0, i).startsWith(boundary))) pw.write(newLine.substring(0, newLine.length()-2).getBytes("ISO8859_1")); else pw.write(newLine.getBytes("ISO8859_1")); newLine = new String(line, 0, i,"ISO8859_1"); } pw.close(); } else { // 普通表单输入元素 // 获取输入元素名字 int pos = newLine.indexOf("name=\""); String fieldName = newLine.substring(pos+6, newLine.length()-3); i = in.readLine(line, 0, 1280); i = in.readLine(line, 0, 1280); newLine = new String(line, 0, i); StringBuffer fieldValue = new StringBuffer(1280); while (i != -1 && !newLine.startsWith(boundary)) { // 最后一行包含换行字符 // 因此我们必须检查当前行是否是最后一行 i = in.readLine(line, 0, 1280); if ((i==boundaryLength+2 || i==boundaryLength+4) && (new String(line, 0, i).startsWith(boundary))) fieldValue.append(newLine.substring(0, newLine.length()-2)); else fieldValue.append(newLine); newLine = new String(line, 0, i); } fields.put(fieldName, fieldValue.toString()); } } i = in.readLine(line, 0, 1280); } } } HTML页面: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>文件上载</title> </head> <body> <form action=jsp1.jsp enctype="MULTIPART/FORM-DATA" method=post> 作者: <input type=text name=author> <br> 公司: <input type=text name=company> <br> 说明: <textarea name=comment></textarea> <br> 选择要上载的文件<input type=file name=filename> <br> 文件描述: <input type=text name=description> <br> <input type=submit value="Upload"> </form> </body> </html> JSP页面: <%@ page contentType="text/html;charset=gb2312"%> <jsp:useBean id="TheBean" scope="page" class="cr.web.FileUploadBean" /> <% TheBean.setSavePath("d:\\"); //TheBean.doUpload1(request); TheBean.doUpload(request); out.println("Filename:" + TheBean.getFilename()); out.println("<BR>内容类型:" + TheBean.getContentType()); out.println("<BR>作者:" + TheBean.getFieldValue("Author")); out.println("<BR>公司:" + TheBean.getFieldValue("Company")); out.println("<BR>说明:" + TheBean.getFieldValue("Comment")); %>