前臺(tái)jsp:<form action="AA.do?method=yktAdd" method="post" ><input type='text' name='textfield' id='textfield' class='txt' /> <input type='button' class='btn' value='瀏覽...' /><input type="file" name="excel" class="file" id="fileField" size="28" onchange="document.getElementById('textfield').value=this.value" /><input type="submit" name="submit" class="btn" value="添加" /></form>后臺(tái):public ActionForward yktAdd(ActionMapping mapping,ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception {String path=request.getParameter("excel");System.out.println(path);return mapping.findForward("yktadd");}程序得到的結(jié)果:XXX.xls我想獲取的結(jié)果是:D:\XXX.xls 這種全路徑只需要一個(gè)路徑就行了,但是查了好多資料,都不成功,求大神解救?。。。?!
5 回答

慕的地3093295
TA貢獻(xiàn)1條經(jīng)驗(yàn) 獲得超0個(gè)贊
獲取文件的絕對(duì)路徑:this.getClass().getClassLoader().getResource("/").getPath();
希望對(duì)你有幫助

滄海一幻覺
TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超5個(gè)贊
使用FormFile這個(gè)類
定義一個(gè)FormBean
public class TestForm{
FormFile file;
private void setFile(FormFile file){
this.file = file;
}
public FormFile getFile(){
return file;
}
}
jsp頁面
<input type="file" name="file">
ActionBean
方法中這樣寫
TestForm tForm = (TestForm)form;// 獲取表單
FormFile file = bForm.getFile();
file.getFilePath(); // 獲取文件路徑
添加回答
舉報(bào)
0/150
提交
取消