标题:[求助]smartupload上传中文问题
只看楼主
angeloc
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:36
帖 子:1353
专家分:0
注 册:2006-11-21
得分:0 
没有代码,我这也不知道确切原因.如果你贴上来我可以帮你看看

老牛明知夕阳晚,不用扬鞭自奋蹄; Angelo\'s BLOG
2007-05-25 09:57
mojianpo
Rank: 1
等 级:等待验证会员
帖 子:213
专家分:0
注 册:2005-7-6
得分:0 

jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'upload.jsp' starting page</title>
<SCRIPT language="javascript">
function addFileRow(){
var objTable = document.getElementById("fileTable");
var objTableBody=objTable.children(0);
var newRow = document.createElement("tr");
var objCell = document.createElement("td");
var objfile = document.createElement("input");
objfile.name="uploadfiles";
objfile.type="file";
objfile.size="50";
//delete
var objDel = document.createElement("input");
objDel.name="del";
objDel.type="button";
objDel.value="Delete";
objDel.onclick = delThis;
objCell.appendChild(objfile);
objCell.appendChild(objDel);
newRow.appendChild(objCell);

objTableBody.appendChild(newRow);
}
function delThis(){//删除一行
var Elm = event.srcElement;
while(Elm && Elm.tagName!="TR"){
Elm = Elm.parentElement;
}
var intThisIndex = Elm.rowIndex;
Elm.parentElement.deleteRow(intThisIndex);
}
function showJoin(actl){//上传组隐藏是否

if(actl.checked==true){
document.getElementById("uploadtr").style.display='none';
} else {
document.getElementById("uploadtr").style.display='';
}
}
function fsubmit(){

document.forms[0].action ="/FrameSetPro/upFile.do"
document.forms[0].submit();
}
</SCRIPT>
</head>

<body>
<form name="form1" enctype="multipart/form-data" method="post">
<table width="500" border="0" cellpadding="0" cellspacing="1" bgcolor="#FF6600">
<tr>
<td colspan="2" align="left" valign="top"><table width="100%" height="29%" border="0" cellpadding="0" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td align="center" valign="middle">文件进行上传</td>
<td align ="right"><INPUT type="checkbox" name="cbx_hidden" onclick="javascript:showJoin(this)">隐藏&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:addFileRow();">添加</a></td>
</tr>
<tr id="uploadtr" style="display:''">
<td colspan="2"><table width="490" border="1" cellpadding="3" cellspacing="0"
id="fileTable">

</table></td>
</tr>
</table> </td>
</tr>
<tr>
<td align="center"><input type="button" name="fsub" value="提交" onclick="javascript:fsubmit();"></td>
<td align="center"><input type="button" name="btn_clsoe" value="关闭" onclick="javascript:window.close();"></td>
</tr>
</table>
</form>
</body>
</html>


我是莫建坡.电话:15910657130
2007-05-25 12:01
mojianpo
Rank: 1
等 级:等待验证会员
帖 子:213
专家分:0
注 册:2005-7-6
得分:0 

Action :

//Created by MyEclipse Struts
// XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.0.0/xslt/JavaClass.xsl

package com.mjp.updown.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import smart.SmartFile;
import smart.SmartUpload;


/**
* MyEclipse Struts Creation date: 05-23-2007
*
* XDoclet definition:
*
* @struts.action validate="true"
*/
public class UpFileAction extends Action {

// --------------------------------------------------------- Instance
// Variables

// --------------------------------------------------------- Methods

/**
* Method execute
*
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
* @throws UploadException
* @throws IOException
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws IOException {

// 新建一个SmartUpload对象
SmartUpload su = new SmartUpload();

List list = new ArrayList();
ServletConfig config = this.getServlet().getServletConfig();


// 上传初始化
try {
su.initialize(config, request, response);
// 设置上传文件允许的最大尺寸为2M
su.setMaxFileSize(2 * 1024 * 1024);
su.setAllowedFilesList("doc,txt,xls");
su.setDeniedFilesList("exe,bat,jsp,htm,html,,");
su.upload();
} catch (Exception ex) {
return mapping.findForward("failure");
}

// 逐一提取上传文件信息,同时可保存文件。
for (int i = 0; i < su.getFiles().getCount(); i++) {
SmartFile file = (su.getFiles().getFile(i));

// 若文件不存在则继续
if (file.isMissing())
continue;
StringBuffer buf = new StringBuffer();
// 显示当前文件信息
buf.append("<TABLE BORDER=1>");
buf.append("<TR><TD>表单项名(FieldName)</TD><TD>" + file.getFieldName()
+ "</TD></TR>");
buf.append("<TR><TD>文件长度(Size)</TD><TD>" + file.getSize()
+ "</TD></TR>");
buf.append("<TR><TD>文件名(FileName)</TD><TD>" + file.getFileName()
+ "</TD></TR>");
buf.append("<TR><TD>文件扩展名(FileExt)</TD><TD>" + file.getFileExt()
+ "</TD></TR>");
buf.append("<TR><TD>文件全名(FilePathName)</TD><TD>" + file.getFilePathName()
+ "</TD></TR>");
buf.append("</TABLE><BR>");
list.add(buf);

String fileName = new String(file.getFileName().getBytes(),"GBK");
System.out.println(fileName);

// 将文件另存
// file.saveAs("/upload/" + myFile.getFileName());
// 另存到以WEB应用程序的根目录为文件根目录的目录下
try {

file.saveAs("/fare/"+file.getFileName(), su.SAVE_VIRTUAL);
} catch (Exception ex) {
System.out.println(ex);
return mapping.findForward("failure");
}
// 另存到操作系统的根目录为文件根目录的目录下
// file.saveAs("c:\\temp\\" + myFile.getFileName(),
// su.SAVE_PHYSICAL);

}
request.setAttribute("list", list);
return mapping.findForward("success");
}
public static String toUtf8String(String s) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c >= 0 && c <= 255) {
sb.append(c);
} else {
byte[] b;
try {
b = Character.toString(c).getBytes("utf-8");
} catch (Exception ex) {
System.out.println(ex);
b = new byte[0];
}
for (int j = 0; j < b.length; j++) {
int k = b[j];
if (k < 0)
k += 256;
sb.append("%" + Integer.toHexString(k).toUpperCase());
}
}
}
return sb.toString();
}//end

}


我是莫建坡.电话:15910657130
2007-05-25 12:02
angeloc
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:36
帖 子:1353
专家分:0
注 册:2006-11-21
得分:0 
看来这个问题还是编码问题,因为你的页面是UTF-8的,这样你要么修改SmartUpload的代码把UTF-8转码,要么你可以试试用过滤器过滤这个上传的SERVLET,把UTF-8转为GBK或别的!
而且这个根你的服务器和容器也有关系,如果你的服务器和容器不支持中文,你只能转成其他编码,下载的时候再转回来!

老牛明知夕阳晚,不用扬鞭自奋蹄; Angelo\'s BLOG
2007-05-25 14:03
mojianpo
Rank: 1
等 级:等待验证会员
帖 子:213
专家分:0
注 册:2005-7-6
得分:0 

谢谢了,我在研究研究


我是莫建坡.电话:15910657130
2007-05-25 14:46



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-142165-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.797156 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved