搞了半天也没高顶,就是在上传中文文件时,路径是乱码,
我用tring fileName = new String(file.getFileName().getBytes(),"UTF-8");
但最后的一个中文是乱吗,杂解决?
谢谢!!
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)">隐藏 <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>
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
}