您的位置:首页 > 其它

关于使用二进制流上传的问题,求大神解答

2017-01-08 18:11 274 查看
本项目使用ssh开发,上传功能的时候上传的附件如果超过2个,则action直接返回input.里面的上传方法也不走,附件是2个或者2个一下的时候则没问题。本人不知道是什么原因。求解答。

我的代码:

Action的:

public class FileAction extends ActionSupport {

// 上传文件存放路径

private final static String UPLOADDIR = "/upload";

// 上传文件集合

private List<File> file;

// 上传文件名集合

private List<String> fileFileName;

// 上传文件内容类型集合

private List<String> fileContentType;

private Integer txtid;

public Integer getTxtid() {

return txtid;

}

public void setTxtid(Integer txtid) {

this.txtid = txtid;

}

private TblTextDao tdao;

private TblText ttxt;

public List<File> getFile() {

return file;

}

public void setFile(List<File> file) {

this.file = file;

}

public List<String> getFileFileName() {

return fileFileName;

}

public void setFileFileName(List<String> fileFileName) {

this.fileFileName = fileFileName;

}

public List<String> getFileContentType() {

return fileContentType;

}

public void setFileContentType(List<String> fileContentType) {

this.fileContentType = fileContentType;

}

public TblTextDao getTdao() {

return tdao;

}

public void setTdao(TblTextDao tdao) {

this.tdao = tdao;

}

public TblText getTtxt() {

return ttxt;

}

public void setTtxt(TblText ttxt) {

this.ttxt = ttxt;

}

public String execute() throws Exception {

List<String> listpath = new ArrayList<String>();

Map session = ActionContext.getContext().getSession();

List<Boolean> listbool = new ArrayList<Boolean>();

if (file != null) {

if (file.size() > 0) {

for (int i = 0; i < file.size(); i++) {

// 循环上传每个文件

boolean b = upload(i,listpath);

if (b = false) {

listbool.add(b);

}

}

}

}

if (listbool.size() == 0) {

String paths = "";

for (int i = 0; i < listpath.size(); i++) {

if (listpath.size() == 1) {

paths = listpath.get(i);

} else {

paths = paths + "|" + listpath.get(i);

}

}

ttxt.setImagePath(paths);

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

ttxt.setTextCreatTime(formatter.format(new Date()));

TblUser u = new TblUser();

u = (TblUser) session.get("user");

ttxt.setTblUser(u);

boolean b = tdao.addText(ttxt);

if (b) {

session.put("message", "添加成功");

return "addSuccess";

} else {

session.put("message", "对不起,上传失败");

return "addInput";

}

} else {

session.put("message", "对不起,上传失败");

return "addInput";

}

}

public boolean upload(int i,List<String> listpath) {

Map session = ActionContext.getContext().getSession();

try {

InputStream in = new FileInputStream(file.get(i));

String dir = ServletActionContext.getRequest().getRealPath(

UPLOADDIR);

String fileName = this.getFileFileName().get(i);

listpath.add(fileName);

if (fileName.endsWith(".exe")) {

return false;

}

File uploadFile = new File(dir, fileName);

OutputStream out = new FileOutputStream(uploadFile);

byte[] buffer = new byte[1024 * 1024];

int length;

while ((length = in.read(buffer)) > 0) {

out.write(buffer, 0, length);

}

in.close();

out.close();

} catch (FileNotFoundException ex) {

System.out.println("上传失败!");

ex.printStackTrace();

return false;

} catch (IOException ex) {

System.out.println("上传失败!");

ex.printStackTrace();

return false;

}

return true;

}

}

页面代码:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<%@taglib prefix="s" uri="/struts-tags" %>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<script type="text/javascript" src="<%=basePath%>/js/jquery.js"></script>

<script type="text/javascript" src="<%=basePath%>/js/ajaxfileupload.js"></script>

    <base href="<%=basePath%>">

    

    <title>上传文章</title>

    

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">    

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->

<style type="text/css">

body {

margin-left: 0px;

margin-top: 0px;

margin-right: 0px;

margin-bottom: 0px;

font-family: "微软雅黑";

font-size: 16px;

color: #333333;

background-color: #2286C2;

text-align: center;

}

input {

border: 1px solid #2286C2; 

}

.nobr br{display:none}  

</style>

<script type="text/javascript">

function fanhui(){

if(confirm("注:返回后数据将被清空,确认返回?")){

location.href="ManagePage.jsp";

}

}

function yzfrom(){

var title=document.getElementById("title").value;

var content=document.getElementById("content").value;

if(title==null||title==""){

alert("请输入标题");

return false;

}

if(content==null||content==""){

alert("文章内容为空无法上传");

return false;

}

return true;

}

function addFile(){

var filediv=document.getElementById("filediv");

var filelist=document.getElementsByName("file");

var inhtml="<input type='file' id='file' name='file' style='width: 905px;' />";

for(var i=0;i<filelist.length;i++){

inhtml=inhtml+"<br/><input type='file' id='file' name='file' style='width: 905px;' />";

}

filediv.innerHTML=inhtml;

}

</script>

  </head>

  

  <body>

   <div>

   <div id="top" style="background-image: url('images/index1_03.gif'); width: 100%; height: 74px;" ></div>

   <div style="width: 100%">

   <div style="width: 80%; margin-top:100px; height: 500px;" >

   <s:form action="txt.action"  method="post" enctype="multipart/form-data">

<table id="tablefrom">

    <tr><td>所属栏目:</td>

     <td>

     <div class="nobr"><s:doubleselect theme="simple" name="abc" list="#session.clumnzlist" listKey="clumnNo" listValue="clumnText" doubleList="tblClumncs" doubleName="ttxt.tblClumnc.id" doubleListKey="id" doubleListValue="clumnText" ></s:doubleselect></div>

     </td>

     <td style="padding-left: 270px">文章标题:</td>

     <td><input style="width:380px; border:1px solid #FFFFFF" name="
9f00
ttxt.textTitle" id="title" name="ttxt.textTitle" /></td>

    </tr>

<tr></tr>

<tr><td>文章内容:</td><td colspan="3"><textarea rows="" id="content" style="border: 1px solid #FFFFFF; width: 905px; height: 400px; font-family: 微软雅黑" cols="" name="ttxt.textContent"></textarea></td></tr>

<tr></tr>

    <tr><td>上传附件:</td><td colspan="3"><div id="filediv">

    <input type="file" name="file" style="width: 905px;" /><br/>

    </div></td></tr>

    <tr><td colspan="2"><input type="button" onclick="addFile()" value="添加上传框"></td></tr>

    <tr><td colspan="4"><input type="submit" value="上传" id="buttonstyle"  style="border:none; margin-left: 400px; width: 80px; height: 30px;" onclick="return yzfrom()" /><input style="margin-left: 100px; width: 80px; height: 30px;" type="button" value="返回"  id="buttonstyle" onclick="fanhui()" /></td></tr>

<tr><td colspan="4"><div style="color:red">

    <s:fielderror />

</div></td></tr>

</table>

</s:form>

   </div>

   </div>

   </div>

  </body>

</html>

求大神帮忙看下。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐