jsp文件上传获取文件名
① jsp 文件上传和下载
1.jsp页面
<s:form action="fileAction" namespace="/file" method="POST" enctype="multipart/form-data">
<!-- name为后台对应的参数名称 -->
<s:file name="files" label="file1"></s:file>
<s:file name="files" label="file2"></s:file>
<s:file name="files" label="file3"></s:file>
<s:submit value="提交" id="submitBut"></s:submit>
</s:form>
2.Action
//单个文件上传可以用 File files,String filesFileName,String filesContentType
//名称要与jsp中的name相同(三个变量都要生成get,set)
private File[] files;
// 要以File[]变量名开头
private String[] filesFileName;
// 要以File[]变量名开头
private String[] filesContentType;
private ServletContext servletContext;
//Action调用的上传文件方法
public String execute() {
ServletContext servletContext = ServletActionContext.getServletContext();
String dataDir = servletContext.getRealPath("/file/upload");
System.out.println(dataDir);
for (int i = 0; i < files.length; i++) {
File saveFile = new File(dataDir, filesFileName[i]);
files[i].renameTo(saveFile);
}
return "success";
}
3.配置上传文件临时文件夹(在struts.xml中配置)
<constant name="struts.multipart.saveDir" value="c:/temp"/>
文件下载
1.下载的url(到Action)
<a href="${pageContext.request.contextPath}/file/fileAction!down.action">下载</a>
2.struts.xml配置
<package name="file" namespace="/file" extends="struts-default">
<action name="fileAction" class="com.struts2.file.FileAction">
<!-- 下载文件配置 -->
<!--type 为 stream 应用 StreamResult 处理-->
<result name="down" type="stream">
<!--
不管实际类型,待下载文件 ContentType 统一指定为 application/octet-stream
默认为 text/plain
-->
<param name="contentType">application/octet-stream</param>
<!--
默认就是 inputStream,它将会指示 StreamResult 通过 inputName 属性值的 getter 方法,
比如这里就是 getInputStream() 来获取下载文件的内容,意味着你的 Action 要有这个方法
-->
<param name="inputName">inputStream</param>
<!--
默认为 inline(在线打开),设置为 attachment 将会告诉浏览器下载该文件,filename 指定下载文
件保有存时的文件名,若未指定将会是以浏览的页面名作为文件名,如以 download.action 作为文件名,
这里使用的是动态文件名,${fileName}, 它将通过 Action 的 getFileName() 获得文件名
-->
<param name="contentDisposition">attachment;filename="${fileName}"</param>
<!-- 输出时缓冲区的大小 -->
<param name="bufferSize">4096</param>
</result>
</action>
</package>
3.Action
//Action调用的下载文件方法
public String down() {
return "down";
}
//获得下载文件的内容,可以直接读入一个物理文件或从数据库中获取内容
public InputStream getInputStream() throws Exception {
String dir = servletContext.getRealPath("/file/upload");
File file = new File(dir, "icon.png");
if (file.exists()) {
//下载文件
return new FileInputStream(file);
//和 Servlet 中不一样,这里我们不需对输出的中文转码为 ISO8859-1
//将内容(Struts2 文件下载测试)直接写入文件,下载的文件名必须是文本(txt)类型
//return new ByteArrayInputStream("Struts2 文件下载测试".getBytes());
}
return null;
}
// 对于配置中的 ${fileName}, 获得下载保存时的文件名
public String getFileName() {
String fileName ="图标.png";
try {
// 中文文件名也是需要转码为 ISO8859-1,否则乱码
return new String(fileName.getBytes(), "ISO8859-1");
} catch (UnsupportedEncodingException e) {
return "icon.png";
}
}
② 怎样在JSP页面根据上传的文件得到上传文件的文件名
我有源码慎碰哟,还是自己写的哟,要不要
java">privateFileimage;//获取上传文件
privateStringimageFileName;//获取上传文件名称
privateStringimageContentType;//获取上传文件类型
(){
returnimageContentType;
}
publicvoidsetImageContentType(StringimageContentType){
this.imageContentType=imageContentType;
}
publicFilegetImage(){
returnimage;
}
publicvoidsetImage(Fileimage){
this.image=image;
}
publicStringgetImageFileName(){
returnimageFileName;
}
publicvoidsetImageFileName(StringimageFileName){
this.imageFileName=imageFileName;
}
publicStringupload()throwsException{
HttpServletRequestrequest=ServletActionContext.getRequest();
Stringpath宽搏谈=ServletActionContext.getServletContext().getRealPath("/upload");
Stringseconds=System.currentTimeMillis()+"_"+getImageFileName();
if(image!=null){
Filesavefile=newFile(newFile(path),seconds);
if(!savefile.getParentFile().exists())
savefile.getParentFile().mkdirs();
try{
FileUtils.File(image,savefile);
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}
上传一个名为"file.txt"的文件,那么,seconds的值就为: 当前毫秒数_file.txt,银手同时下面判断文件不为空的时候,将文件保存到path中,这里不光是图片可以 其他文件也可以,主要是文件不能为空就能上传成功了
③ 总jspsmartupload组件上传的文件如何获取其文件名
String fileName=smartupload.getFiles().getFile(0).getFileName();
这个有没有扩展名我记此山镇不太清了~~如果现在得到的有扩展名,你又不想森粗要。可以:
int i=fileName.lastIndexOf("."); //这是得到扩展名前面的唯简.下标值。
String fileName=fileName.subString(0,i); //截取从0~ . 之间的的字符就是不含扩展名的文件名了..
如果哪有不妥,再联系我..
④ jsp上传文件时如何获取文件名
在目标页里通过获取request.getInputStream()来得携伏山到一个输厅仿入流。辩中
然后读取到一个byte[]数组里,再还原成String再分析一下;
参考如下:
function
getName(){
var
myFile
=
document.getElementById("myFile").value;
var
length
=
myFile.length;
var
x
=
myFile.lastIndexOf("\\");
x++;
var
fileName
=
myFile.substring(x,length);
document.getElementById("fileName").value
=
fileName;
}
⑤ jsp上传文件名至数据库
你说睁型的是不是不知道怎么样取文件名啊?
在JSP中写:
<jsp:useBean id="smart" scope="page" class="com.jspsmart.upload.SmartUpload"/>
<%
smart.initialize(pageContext) ;
smart.upload() ;
//取得文件名
String fileName = smart.getFiles().getFile(0).getFileName();
smart.save("/uploadfiles") ;
%>
既然你现在有了一个文件名fileName,那么就将它做一个符串保存到数据库中去吧.如果你还没有安装数据库,也没悉判猜有建立过数据库连接,Google一下或去下面冲消的网址看看吧
http://www.west263.com/info/html/chengxusheji/Javajishu/20080225/34458.html
⑥ jsp中怎么取得上传文件的路径和文件名
jsp 代码
<祥含%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<meta http-equiv="pragma"宴轮 content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
</head>
<body>
<table align="center" class="fix_table">
<tr align="center">
<td class="long">
<form action="import.do" method="post"
enctype="multipart/form-data">
上传文件:
<input type="file" name="file1" id="file1"/>
<input type="submit" value="确定" />
</form>
</td>
</tr>
<tr>
<td>
<input type="button" value="获取上传文件" onclick="test();"/>
</td>
</tr>
</table>
</body>
<script type="text/javascript">
function test(){
var file=document.getElementById("file1").value;
alert(file);
}
</script>
</html>
修改浏览谨祥笑器设置
⑦ JAVA如何获取上传后的文件名
同意楼上的分析,另外我想提个建议,代码为了便于阅读可以试着分步骤写。
就先定义filename和filepath,然后你在纤禅后续的操作中想要图片出现在设知稿定的某一区域中
就可以直接使用毁猛尘filename,将要上传的文件名是 its.getIPTimeStampRand()+"."+item.getName().split("\\.")[1]