最近项目组遇到一个文件上传的问题,首先上传的文件需要上传到指定的共享目录(linux),而不是文档服务中心,现将部分代码做分享,希望可以帮助需要的朋友:上传:
1. 在w文件中放form标签,使用平台提供的/UI/system/components/excel/import/upload.j实现文件的上传:
<xhtml:form enctype="multipart/form-data" name="f" id="div-form" action="/UI/system/components/excel/import/upload.j" method="post" target="div-iframe" style="margin:0;"> <xhtml:input name="process" type="hidden" value="/KYBG/secrecySystem/process/extFileExport/extFileExportProcess"/> <xhtml:input name="activity" type="hidden" value="mainActivity"/> <xhtml:input name="action" type="hidden" value="uploadFileAction"/> <xhtml:input name="uploadFile" id="file" type="file" onchange="fileChange()" style="margin-top:1;width:100%;height:20px;border:0px;vertical-align:middle;line-height:18px;"/> </xhtml:form>
<xhtml:iframe id="div-iframe" name="div-iframe" style="display:none" onload="mainActivity.afterUploadFile(this);"/>
2. 新增action
uploadFile为文件输入流,其它参数可在form中自行设置,代码如下:
public static String uploadFile(InputStream uploadFile, String str) { String name = java.util.UUID.randomUUID().toString(); File file = new File(getTempDir() + "/" + name + ".tmp"); if (file.exists()) file.delete(); FileOutputStream output; try { output = new FileOutputStream(file); try { byte[] bs = new byte[1024*4]; int i = -1; while ((i = uploadFile.read(bs)) != -1) { output.write(bs, 0, i); } output.flush(); } finally { output.close(); } } catch (Exception e) { throw new RuntimeException("上传失败!", e); } return "abc"; } private static String getTempDir() { return System.getProperty("java.io.tmpdir"); } form提交时调用方法:
mainActivity.trigger1Click = function(event){ var s = justep.Request.convertURL("/UI/system/components/excel/import/upload.j"); var f = document.getElementById('div-form'); f.setAttribute('action', s); f.attributes['action'].value = s; f.submit(); }; 经过上面的步骤,可以实现选择文件上传,接收并处理文件
3. 获得action的返回值
form提交的返回结果显示在iframe中,action的返回值可以在iframe的onload事件中获取 mainActivity.afterUploadFile = function(iframe){
if (iframe && iframe.contentWindow && iframe.contentWindow.document) {
var doc = (!justep.Browser.IE||justep.Browser.IE9)?iframe.contentWindow.document:iframe.contentWindow.document.XMLDocument;
if(doc){
var data = justep.Request.getData(doc);
var flag = justep.Request.getFlag(doc);
}
if (data && flag) {
if(justep.Browser.IE8)
alert("action返回值:"+data.text);
else
alert("action返回值:"+data.textContent);
}
}
};
下载:
1. 页面上增加一个iframe,通过设置src来完成下载
<xhtml:iframe id="downloadiframe" name="downloadiframe" style="display:none"/> 2. .定义一个action,返回类型是Object
3. 对应的java代码
public static Object downLoadFile(String fileName) throws UnsupportedEncodingException {
FileInputStream fis = null;
String s = java.net.URLDecoder.decode(fileName, "UTF-8");
File file = new File(s);
if (file.getAbsolutePath().startsWith(file.getAbsolutePath())) {
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException e) {
throw new RuntimeException("获取文件失败!", e);
}
} else
throw new RuntimeException("获取文件失败,超出获取文件权限范围!");
return fis;
}
删除比较简单,写一个action,调用一下file.delete()即可。
|