當前位置:首頁 » 文件管理 » jsp文件上傳獲取文件名

jsp文件上傳獲取文件名

發布時間: 2023-04-18 12:34:25

① 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]

熱點內容
qml文件修改後編譯未生效 發布:2025-05-14 07:31:00 瀏覽:330
內到內演算法 發布:2025-05-14 07:29:11 瀏覽:33
文件夾名字不顯示 發布:2025-05-14 07:27:47 瀏覽:774
oracle的資料庫驅動jar 發布:2025-05-14 07:23:20 瀏覽:555
我的世界電腦版伺服器手機版能進嗎 發布:2025-05-14 07:22:01 瀏覽:678
達內培訓php多少錢 發布:2025-05-14 07:19:10 瀏覽:26
python位元組轉字元串 發布:2025-05-14 07:06:35 瀏覽:421
subplotpython 發布:2025-05-14 06:53:51 瀏覽:661
豎屏大屏導航工廠密碼一般是多少 發布:2025-05-14 06:49:29 瀏覽:806
如何在手機里設置無線網密碼 發布:2025-05-14 06:47:54 瀏覽:120