起步软件技术论坛
搜索
 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 1834|回复: 2

[分享] 文件上传到指定目录问题的分享

  [复制链接]
发表于 2013-11-28 21:49:28 | 显示全部楼层 |阅读模式
最近项目组遇到一个文件上传的问题,首先上传的文件需要上传到指定的共享目录(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()即可。

自定义上传文件的方法.rar

49.1 KB, 下载次数: 535

评分

参与人数 1 +10 收起 理由
jishuang + 10 赞一个!

查看全部评分

117

主题

771

帖子

1670

积分

金牌会员

Rank: 6Rank: 6

积分
1670
发表于 2013-11-28 23:44:41 | 显示全部楼层
谢谢分享,楼主的代码论坛管理已经分享过。

本人最近正好也在做文件上传,所以补充几点:
1、return System.getProperty("java.io.tmpdir"); 会获得临时目录,如果需要自己指定目录,最好是把java.io.tmpdir设为null 。
2、 在File file = new File(getTempDir() + "/" + name + ".tmp")中修改指定的目录,如 File file = new File(getTempDir() + "/目录路径/" + name + ".tmp");
3、 File file = new File(getTempDir() + "/" + name + ".tmp")为上传后的文件扩展名为tmp,有时候需要指定上传后的文件扩展名,需要修改.tmp,  如上传后的文件指定为GIF图片,则修改为                              File file = new File(getTempDir() + "/" + name + ".GIF")。
4、如果需要把上传后的文件名保存到数据库,则需要在mainActivity.afterUploadFile中进行赋值,如:
    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);
                  justep.xbl('dataMain').setValue("hf6pic", data.text);   //赋值文件名
                    else
                        alert("action返回值:"+data.textContent);        
                    justep.xbl('dataMain').setValue("hf6pic", data.textContent);      //赋值文件名
               }
        }
};
   

评分

参与人数 1 +10 收起 理由
jishuang + 10 赞一个!

查看全部评分

本人非X5官方技术,回复内容仅供参考。
回复 支持 反对

使用道具 举报

117

主题

771

帖子

1670

积分

金牌会员

Rank: 6Rank: 6

积分
1670
发表于 2013-11-28 23:48:44 | 显示全部楼层
另外说一点关于富文本中的图片上传问题,5.2.6版本的富文本使用百度编辑器,图片上传已经变更为上传到文档服务器中,5.2.6之前的版本是上传到指定目录,机制已经改变,请升级的朋友们特别注意这点。


本人非X5官方技术,回复内容仅供参考。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|X3技术论坛|Justep Inc.    

GMT+8, 2024-5-18 13:16 , Processed in 0.094508 second(s), 31 queries .

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表