<head>
<meta http-equiv="Content-Type" content="text/htmlcharset=gb2312">
<title>无标题文档</title>
</head>
<body>
<%
'On Error Resume Next
Response.Expires=0
if Request.TotalBytes then
set a=createobject("adodb.stream")
a.Type=1
a.Open
a.write Request.BinaryRead(Request.TotalBytes)
a.Position=0
b=a.Read
c=chrB(13)&chrB(10)
d=clng(instrb(b,c))
e=instrb(d+1,b,c)
set f=createobject("adodb.stream")
f.type=1
f.open
a.Position=d+1
a.copyto f,e-d-3
f.Position=0
f.type=2
f.CharSet="GB2312"
g=f.readtext
f.Close
h=mid(g,instrRev(g,"\")+1,e)
i=instrb(b,c&c)+4
j=instrb(i+1,b,leftB(b,d-1))-i-2
if j <1 then
set f =nothing
set a =nothing
response.write "未选择要上传的文件<a href='?'>重新上传</a>"
response.end
end if
f.Type=1
f.Open
a.Position=i-1
a.CopyTo f,j
h = Mid(h, InStrRev(h, "filename=""") + 10) '这是我帮你添加的,文件名的获取没有正确
f.SaveToFile server.mappath("/EXCEL/"&h),2
f.Close
set f=Nothing
a.Close
set a=Nothing
'response.write "<a href="&Server.URlEncode(h)&">"&h&"</a>"
end if
If Err.number<>0 Then
response.Write err.number
response.Write err.Description
Response.End
End If
%>
<script language="javascript">
function checkupload() {
if (document.upload_form.fe.value == "") {
alert("未选择要上传的文件")
return false
}
}
</script>
<form name="upload_form" enctype="multipart/form-data" method="post" onsubmit="return(checkupload())">
<input type="file" name="fe"/>
<input type="submit" value="上传" name="B1"/>
</form>
</body>
</html>
文件从本地到服务器的功能,其实是为了解决目前浏览器不支持获取本地文件全路径。不得已而想到上传到服务器的固定目录,从而方便项目获取文件,进而使程序支持EXCEL批量导入数据。
java中文件上传到服务器的指定路径的代码:
在前台界面中输入:
<form method="post" enctype="multipart/form-data" action="../manage/excelImport.do">
请选文件:<input type="file" name="excelFile">
<input type="submit" value="导入" onclick="return impExcel()"/>
</form>
action中获取前台传来数据并保存
/**
* excel 导入文件
* @return
* @throws IOException
*/
@RequestMapping("/usermanager/excelImport.do")
public String excelImport(
String filePath,
MultipartFile excelFile,HttpServletRequest request) throws IOException{
log.info("<<<<<<action:{} Method:{} start>>>>>>","usermanager","excelImport" )
if (excelFile != null){
String filename=excelFile.getOriginalFilename()
String a=request.getRealPath("u/cms/www/201509")
SaveFileFromInputStream(excelFile.getInputStream(),request.getRealPath("u/cms/www/201509"),filename)//保存到服务器的路径
}
log.info("<<<<<<action:{} Method:{} end>>>>>>","usermanager","excelImport" )
return ""
}
/**
* 将MultipartFile转化为file并保存到服务器上的某地
*/
public void SaveFileFromInputStream(InputStream stream,String path,String savefile) throws IOException
{
FileOutputStream fs=new FileOutputStream( path + "/"+ savefile)
System.out.println("------------"+path + "/"+ savefile)
byte[] buffer =new byte[1024*1024]
int bytesum = 0
int byteread = 0
while ((byteread=stream.read(buffer))!=-1)
{
bytesum+=byteread
fs.write(buffer,0,byteread)
fs.flush()
}
fs.close()
stream.close()
}
//文件写入流private void ReadFile()
{
Byte[] MesageFile
string path =@"c:\123.XML"
FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read)
int size = Convert.ToInt32(stream.Length)
MesageFile = new Byte[size]
stream.Read(MesageFile, 0, size)
stream.Close()
string fileName =path.Substring(path.LastIndexOf("\\") + 1, path.Length path.LastIndexOf("\\") - 1)
WriteFile(MesageFile, fileName)
}
//写入文件
private void WriteFile(Byte[] fileByte,string fileName)
{
string path = AppDomain.CurrentDomain.BaseDirectory + "\\UpLoad\\" + DateTime.Now.ToString("yyyy-MM-dd")+"\\"
if (!Directory.Exists(path))
Directory.CreateDirectory(path)
string savepath = path + fileName
FileStream fos = new FileStream(savepath, FileMode.OpenOrCreate, FileAccess.ReadWrite)
fos.Write(MesageFile, 0, MesageFile.Length)
fos.Close()
}
上传的文件格式不限。
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)