转自:http://www.linuxsir.org/bbs/archive/index.php/t-167492.html
LinuxSir.Org > 编程开发讨论区 —— LinuxSir.Org > Java 程序设计开发讨论 > 我想用jsp上传含中文名的文件,该怎么办??
--------------------------------------------------------------------------------
PDA查看完整版本 : 我想用jsp上传含中文名的文件,该怎么办??
--------------------------------------------------------------------------------
pilchard04-12-30, 11:24
上传到服务器以后中文显示不出来?
--------------------------------------------------------------------------------
eTony04-12-30, 11:42
我用的 o'reilly 的上传组件 没有问题
--------------------------------------------------------------------------------
nscwf04-12-30, 12:06
我有个办法,把文件名用POST记下来,上传之后,再更名回来
--------------------------------------------------------------------------------
hantsy04-12-30, 13:42
我用struts上传没有问题,
--------------------------------------------------------------------------------
fangshun05-01-06, 08:47
上传的字节流转换String时需要转码!!!
--------------------------------------------------------------------------------
fangshun05-01-06, 08:53
/**
* 文件上传进行代理服务,阻止不符合上传规范的文件进入。
* 使用单实例创建,外部调用只有通过工厂方法才可以创建唯一的代理实例。
* @author fangshun
* @see FileUploadFactroy
*/
public class FileUploadProxy implements FileUpload {
/**
* 外部调用只有通过工厂方法才可以创建唯一的代理实例。
*/
protected FileUploadProxy() {}
/**
* 上传方法<tt>upload</tt>的代理验证,阻止不符合规范的上传方式,
* 实现接口的方法。
* @param request 需要得到从上传页面传来的request对象。
*
* @param pathName 服务器存放上传文件的绝对路径,
* 这可能来自你自己的指定,或者来自你要解析的某个已经配置好的配置文件
* 例如WEB-INF下的某个目录的xml配置文件
*
* @param sizeMax 需要上传的文件总体的容量,这里使用long类型,以满足
* 能支持大容量的文件上传,请注意:上传参数sizeMax的最小单位为'K'
*
* @throws Exception
* @see FileUpload
*/
public void upload(HttpServletRequest request, String pathName, long sizeMax)
throws Exception {
// TODO Auto-generated method stub
if ((request == null) || (pathName == null)) {
throw new NullPointerException("parameter");
}
if (sizeMax < 0) {
throw new FileSizeException("File Require Size ");
} else {
//从request中获取上下文内容尺度
int requestSize = request.getContentLength();
//为传来的最大容量乘以1024,获得符合以'K'为单位的尺度
long sizeMax_K = sizeMax << 10;
//String pathNameCN = new String(pathName.getBytes("utf-8"), "gb2312");
//如果传来的尺度大于上传的最大限度
if (requestSize > sizeMax_K) {
throw new SizeLimitExceededException(
"the request was rejected because " +
"it's size exceeds allowed range");
}
String contentType = request.getContentType();
//如果传来的条件符合代理类的要求,执行上传操作!!!
JetStepFileUpload upload = JetStepFileUpload.getInstance();
upload.processUpload(request, pathName, sizeMax_K);
}
}
/**
* 上传实现细节
* 内隐类实现
* @author fangshun
*/
private static class JetStepFileUpload {
private static JetStepFileUpload upload;
/**
* 外界对象不能进行实例化,私有创建对象。
*/
private JetStepFileUpload() { }
private static JetStepFileUpload getInstance() {
if(upload == null) {
upload = new JetStepFileUpload();
}
return upload;
}
/**
* 上传实现
* @param request 需要得到从上传页面传来的request对象。
*
* @param pathName 服务器存放上传文件的绝对路径,
* 这可能来自你自己的指定,或者来自你要解析的某个已经配置好的配置文件
* 例如WEB-INF下的某个目录的xml配置文件
*
* @param sizeMax 需要上传的文件总体的容量,这里使用long类型,以满足
* 能支持大容量的文件上传,参数sizeMax的最小单位为'K'
* @throws Exception
*/
private void processUpload(HttpServletRequest request, String filePath, long sizeMax)
throws Exception {
DiskFileUpload fu = new DiskFileUpload();
String encoding = request.getCharacterEncoding();
fu.setHeaderEncoding(encoding);
// If file size exceeds, a FileUploadException will be thrown
fu.setSizeMax(sizeMax);
List fileItems = fu.parseRequest(request);
Iterator itr = fileItems.iterator();
while (itr.hasNext()) {
FileItem fi = (FileItem) itr.next();
String fileName = fi.getName();
if(fileName == null || fileName.equals("")) {
continue;
}
System.out.println("no convert" + fileName);
//fileName = new String(fileName.getBytes(), "utf-8");
// fileName = new String(fileName.getBytes(), "gb2312");
System.out.println("fileName" + fileName);
//Check if not form field so as to only handle the file inputs
//else condition handles the submit button input
if (!fi.isFormField()) {
File file = new File(fileName);
File fNew = new File(filePath, file.getName());
fi.write(fNew);
} else {
System.out.println("Field =" + fi.getFieldName());
}
}
}
}
}
--------------------------------------------------------------------------------
L0veyou05-01-07, 20:36
这是一个用Bean的文件上传JSP,可以识别各种编码.
uploadfile.jsp:
<%@ page language="java"%>
<%@ page contentType="text/HTML;charset=GB2312"%>
<jsp:useBean id="fub" scope="page" class="mybean.FileUploadBean" />
<%
String Dir = "D:\\WWW\\testing\\test"; //保存文件的路径,请确保目录存在,或改到其他目录
//通过调用JavaBeans的方法完成上传操作
fub.setUploadDirectory(Dir);
fub.uploadFile(request);
out.print("<center><font color=red>文件上载成功!</font></center>");
%>
FileUploadBean.java
package mybean;
import java.io.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletException;
public class FileUploadBean{
private static String newline = "\r\n"; //设定换行符
private String uploadDirectory = "."; //默认的保存位置
private String ContentType = ""; //文件类型
private String CharacterEncoding = ""; //编码格式
//设定文件要保存在服务器中的位置
public void setUploadDirectory(String s){
uploadDirectory = s;
}
//提取文件名,本方法是把用户上传的文件按照原名保存
private String getFileName(String s){
int i = s.lastIndexOf("\\");
if(i < 0 || i >= s.length() - 1){
i = s.lastIndexOf("/");
if(i < 0 || i >= s.length() - 1)
return s;
}
return s.substring(i + 1);
}
//得到content-type
public void setContentType(String s){
ContentType = s;
int j;
if((j = ContentType.indexOf("boundary=")) != -1){
ContentType = ContentType.substring(j + 9);
ContentType = "--" + ContentType;
}
}
//得到文件编码格式
public void setCharacterEncoding(String s){
CharacterEncoding = s;
}
public void uploadFile( HttpServletRequest req) throws ServletException, IOException{
setCharacterEncoding(req.getCharacterEncoding());
setContentType(req.getContentType());
uploadFile(req.getInputStream());
}
public void uploadFile( ServletInputStream servletinputstream) throws ServletException, IOException{
String s5 = null;
String filename = null;
byte Linebyte[] = new byte[4096];
byte outLinebyte[] = new byte[4096];
int ai[] = new int[1];
int ai1[] = new int[1];
String line;
//得到文件名
while((line = readLine(Linebyte, ai, servletinputstream, CharacterEncoding)) != null){
int i = line.indexOf("filename=");
if(i >= 0){
line = line.substring(i + 10);
if((i = line.indexOf("\"")) > 0)
line = line.substring(0, i);
break;
}
}
filename = line;
if(filename != null && !filename.equals("\"")){
filename = getFileName(filename);
String sContentType = readLine(Linebyte, ai, servletinputstream, CharacterEncoding);
if(sContentType.indexOf("Content-Type") >= 0)
readLine(Linebyte, ai, servletinputstream, CharacterEncoding);
//建立新文件的文件句柄
File file = new File(uploadDirectory, filename);
//建立生成新文件的输出流
FileOutputStream fileoutputstream = new FileOutputStream(file);
while((sContentType = readLine(Linebyte, ai, servletinputstream, CharacterEncoding)) != null){
if(sContentType.indexOf(ContentType) == 0 && Linebyte[0] == 45)
break;
if(s5 != null){
//写入新文件
fileoutputstream.write(outLinebyte, 0, ai1[0]);
fileoutputstream.flush();
}
s5 = readLine(outLinebyte, ai1, servletinputstream, CharacterEncoding);
if(s5 == null || s5.indexOf(ContentType) == 0 && outLinebyte[0] == 45)
break;
fileoutputstream.write(Linebyte, 0, ai[0]);
fileoutputstream.flush();
}
byte byte0;
if(newline.length() == 1)
byte0 = 2;
else
byte0 = 1;
if(s5 != null && outLinebyte[0] != 45 && ai1[0] > newline.length() * byte0)
fileoutputstream.write(outLinebyte, 0, ai1[0] - newline.length() * byte0);
if(sContentType != null && Linebyte[0] != 45 && ai[0] > newline.length() * byte0)
fileoutputstream.write(Linebyte, 0, ai[0] - newline.length() * byte0);
fileoutputstream.close();
}
}
//readLine函数把表单提交上来的数据按行读取
private String readLine(byte Linebyte[], int ai[],ServletInputStream servletinputstream,String CharacterEncoding){
try{ //读取一行
ai[0] = servletinputstream.readLine(Linebyte, 0, Linebyte.length);
if(ai[0] == -1)
return null;
}catch(IOException ex){
return null;
}
try{
if(CharacterEncoding == null){
//用默认的编码方式把给定的byte数组转换为字符串
return new String(Linebyte, 0, ai[0]);
}else{
//用给定的编码方式把给定的byte数组转换为字符串
return new String(Linebyte, 0, ai[0], CharacterEncoding);
}
}catch(Exception ex){
return null;
}
}
}
--------------------------------------------------------------------------------
vBulletin v3.5.4,版权所有 ©2000-2006,Jelsoft Enterprises Ltd.