关于获取<input type='file'>获取文件的路径问题

9 篇文章 0 订阅

想做一个根据用户提供的文件地址,然后根据文件的路径对文件进行检测,但是弄了好久发现仅仅只能获取到文件名,不能获取到文件的所在路径,查了好多资料,无果,所以换了种方法来实现。借用Struts2中的fileupload功能。

<form action="fileInfo" method="post" enctype="multipart/form-data">
<table>
<tr>
<td>请选择待检测的文件:</td>
<td><input type="file" name="filename" ></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="提交"></td>
</tr>
</table>
</form>


在action文件中写上

package com.emptyResult;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;


import com.opensymphony.xwork2.ActionSupport;


public class FileInfo extends ActionSupport{
/**

*/
private static final long serialVersionUID = 1L;
File
filename; //获取上传的文件;
String filenameContentType
;//获取上传文件的文件类型;
String filenameFileName
;//获取上传文件的名称;

public File getFilename() {
return filename;
}
public void setFilename(File filename) {
this.filename = filename;
}
public String getFilenameContentType() {
return filenameContentType;
}

public void setFilenameContentType(String filenameContentType) {
this.filenameContentType = filenameContentType;
}

public String getFilenameFileName() {
return filenameFileName;
}
public void setFilenameFileName(String filenameFileName) {
this.filenameFileName = filenameFileName;
}

@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new FileReader(filename));
String str;
while((str = br.readLine())!=null)
System.out.println(str);

return "success";
}
}
   

利用Struts2中的commons-fileupload-1.3.1.jar 提供的功能,但是一定要记住在action中要声明3个变量,变量的命名也是有一定的规定,红色注释部分的名称应该一致。

可能是由于安全的考虑,直接获取上传文件的本地地址行不通,此时可以对上传的文件进行相应的文件处理,相当于文件的副本被上传,所以我们想对文件的任何处理都可以通过处理这个文件的副本来完成。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<td contenteditable="true" ><input type="text" oninput="filterTable(1)" placeholder="aoiStep" ></td> <td contenteditable="true" ><input type="text" oninput="filterTable(2)" placeholder="defectType" ></td> <td contenteditable="true" ><input type="text" oninput="filterTable(3)" placeholder="layerCode" ></td> <td contenteditable="true" ><input type="text" oninput="filterTable(4)" placeholder="type"></td> <td contenteditable="true" ><input type="text" oninput="filterTable(5)" placeholder="dpet" ></td> <td contenteditable="true"><input type="text" oninput="filterTable(6)" placeholder="subcode" ></td> <td contenteditable="true" ><input type="text" placeholder="codeDescription" ></td> <td contenteditable="true" >image1</td> <td contenteditable="true" >image2</td> <td contenteditable="true">image3</td> <td contenteditable="true" >image4</td> <td contenteditable="true" >image5</td> <td contenteditable="true" ><input type="text" placeholder="determination_rule"></td> </tr> </thead> <tbody> <tr> <td contenteditable="true" onclick="selectCell(this)">ACT</td> <td contenteditable="true" onclick="selectCell(this)">Particle</td> <td contenteditable="true" onclick="selectCell(this)">ACT</td> <td contenteditable="true" onclick="selectCell(this)">Particle</td> <td contenteditable="true" onclick="selectCell(this)">ACT</td> <td contenteditable="true" onclick="selectCell(this)">Particle</td> <td contenteditable="true" onclick="selectCell(this)">ACT</td> <td> <input type="file" onchange="previewImage(this);selectCell(this)" onclick="showPopup(this.src)" style="width: 100px; height: auto;"> <td> <input type="file" onchange="previewImage(this);selectCell(this)" onclick="showPopup(this.src)" style="width: 100px; height: auto;"> </td> <td> <input type="file" onchange="previewImage(this);selectCell(this)" onclick="showPopup(this.src)" style="width: 100px; height: auto;"> </td> <td> <input type="file" onchange="previewImage(this);selectCell(this)" onclick="showPopup(this.src)" style="width: 100px; height: auto;"> </td> <td> <input type="file" onchange="previewImage(this);selectCell(this)" onclick="showPopup(this.src)" style="width: 100px; height: auto;"> </td> <td contenteditable="true" onclick="selectCell(this)">Particle</td> 这我的表头,以及表格填充行,我现在需要在保持我现有的功能情况下需要对表格进行修改为以下格式,确保数据能偶提交保存到数据库内,并且能将图片行图片保存在电脑本地,后将图片路径保存在数据库内<form action="upload.php" method="POST" enctype="multipart/form-data"> <!-- 表单内容 --> <input type="file" name="image1"> <!-- 其他字段 --> <button type="submit">提交</button> </form>
最新发布
07-23
根据你的要求,以下是修改后的代码示例: ```html <form action="upload.php" method="POST" enctype="multipart/form-data"> <table> <thead> <tr> <th>aoiStep</th> <th>defectType</th> <th>layerCode</th> <th>type</th> <th>dpet</th> <th>subcode</th> <th>codeDescription</th> <th>image1</th> <th>image2</th> <th>image3</th> <th>image4</th> <th>image5</th> <th>determination_rule</th> </tr> </thead> <tbody> <tr> <td contenteditable="true"><input type="text" name="aoiStep" placeholder="aoiStep"></td> <td contenteditable="true"><input type="text" name="defectType" placeholder="defectType"></td> <td contenteditable="true"><input type="text" name="layerCode" placeholder="layerCode"></td> <td contenteditable="true"><input type="text" name="type" placeholder="type"></td> <td contenteditable="true"><input type="text" name="dpet" placeholder="dpet"></td> <td contenteditable="true"><input type="text" name="subcode" placeholder="subcode"></td> <td contenteditable="true"><input type="text" name="codeDescription" placeholder="codeDescription"></td> <td><input type="file" name="image1" style="width: 100px; height: auto;"></td> <td><input type="file" name="image2" style="width: 100px; height: auto;"></td> <td><input type="file" name="image3" style="width: 100px; height: auto;"></td> <td><input type="file" name="image4" style="width: 100px; height: auto;"></td> <td><input type="file" name="image5" style="width: 100px; height: auto;"></td> <td contenteditable="true"><input type="text" name="determination_rule" placeholder="determination_rule"></td> </tr> </tbody> </table> <button type="submit">提交</button> </form> ``` 在这个修改后的代码中,我将图片上传字段添加到每个对应的输入框中,并为每个文件选择框添加了 `name` 属性,确保在提交表单时能够正确传递对应的文件数据。 请注意,你需要根据实际情况修改表格的样式和布局,以及表单的目标 URL(`action` 属性)和其他字段。 当你提交表单时,所选图片将会被上传到 `upload.php` 脚本中进行处理。你可以在 `upload.php` 中使用 PHP 代码来处理图片上传并将文件保存到服务器或数据库中。 希望这个回答对你有帮助!如果你还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值