struts 2アップロードダウンロード
4075 ワード
アップロードjsp部分コード
//アップロードパスが有効かどうかを簡単に判断するname=「forms」
uploadAction.java
ダウンロード
//削除
//ダウンロードキャンセルエラー
<td > :</td>
<td >
<s:file name="[color=brown]file[/color]" id="Jiafile"/>
</td>
//アップロードパスが有効かどうかを簡単に判断するname=「forms」
function sub(){
var filespec = document.forms[0].file.value;
var patrn=/^[C|D|E|F]:\\.+$/;
if (!patrn.exec(filespec)){
alert( " !");
return ;
} else{
document.forms[0].action="upload.action";
document.forms[0].submit();
}
}
uploadAction.java
private java.io.File file; //file jsp file name
private String fileFileName; // file
private String path="/upload"; // WebRoot upload
public String upload() {
//
String xname=UUID.randomUUID().toString()+fileFileName.substring( fileFileName.lastIndexOf("."));
//
.....
//
this.send(file, xname );
return SUCCESS;
}
/**
* name
*/
public void send( File file , String name) throws Exception {
//
FileOutputStream fos = new FileOutputStream(
ServletActionContext.getServletContext().getRealPath(path) + "\\" + name);
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0)
{
fos.write(buffer , 0 , len);
}
fos.close();
fis.close();
}
ダウンロード
private String inputPath ;
private String filename;
private String id ;
public InputStream getInputStream() throws Exception {
return ServletActionContext.getServletContext().getResourceAsStream(inputPath);
}
public String downloadFile() throws IOException {
FileManager service = new FileManager(this.getLoginId());
cn.com.harbor.oa.file.entity.File oaFile = service.getFileById(id);
oaFile.setNumOfDownload(oaFile.getNumOfDownload() + 1);
service.update(oaFile);
inputPath="/user/file/upload/"+oaFile.getXname();//
this.filename= new String( oaFile.getName().getBytes("GBK"),"ISO8859-1"); //
HttpServletResponse response = ServletActionContext.getResponse();
return null;
}
<action name="downloadFile" class="cn.com.harbor.oa.file.FileUploadAction" method="downloadFile">
<result name="success" type="stream">
<!-- param name="contentType">${contentType}</param-->
<param name="inputName">inputStream</param>
<param name="bufferSize">4096</param>
<param name="contentDisposition">attachment;filename="${filename}"</param>
<param name="">/user/file/fileUpload.jsp</param>
</result>
</action>
//削除
/**
*
*/
public boolean delPic( String xname )
{
File file = new File(ServletActionContext.getServletContext().getRealPath(path)+"/"+xname);
if(!file.exists()) {
return false;
} else {
if(file.exists() && file.isFile()) return file.delete();
else return false;
}
}
//ダウンロードキャンセルエラー