一个上传文件的问题,up有分.分不够再加

上海老猫 2004-11-22 10:29:37
我用获得一个文件名,现在要上传该文件.怎么做?提供思路也可以.当然最好是代码.
...全文
277 26 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
上海老猫 2004-11-23
  • 打赏
  • 举报
回复
to 阿宝:我是要自动上传文件,又不能用<input type="file">.如何取得文件流呀
上海老猫 2004-11-23
  • 打赏
  • 举报
回复
我试试,先谢了
wangchq 2004-11-23
  • 打赏
  • 举报
回复
用apache的commons fileupload 组件,很方便.底层都已经封装好了.
上海老猫 2004-11-23
  • 打赏
  • 举报
回复
还有文件是客户端的,我怎么读进buffer
上海老猫 2004-11-23
  • 打赏
  • 举报
回复
to 陈陈:你的意思是生成流,再在服务器另外生成一个文件
lgdczm 2004-11-23
  • 打赏
  • 举报
回复
读进buffer里,一次传完,得到文件名和路径即可,读完返回内容
上海老猫 2004-11-23
  • 打赏
  • 举报
回复
to 钥匙:
那如何生成文件的二进制流?
jerrykey 2004-11-23
  • 打赏
  • 举报
回复
输入输出流的问题……
上海老猫 2004-11-23
  • 打赏
  • 举报
回复
to最终幻想:用jspsmartupload如何只知道文件名就可以上传?
上海老猫 2004-11-23
  • 打赏
  • 举报
回复
to aoplo(新札师弟)
我是用execCommand()的方式插入图片后,再取得图片路径.再上传,我不知道要上传几个,是什么文件.然后想用<div id="filename"></div>
<script>filename.innerHTML='<input type="file" value="'+filePath+'>'</script>但这样做不行.你有什么好办法?
上海老猫 2004-11-22
  • 打赏
  • 举报
回复
<input type="file">不能用代码赋值,只能手动
fashchina 2004-11-22
  • 打赏
  • 举报
回复
up
funcreal 2004-11-22
  • 打赏
  • 举报
回复
何谓 但是我又不能用代码写<input type="file" value="file:///c:/a:gif">
上海老猫 2004-11-22
  • 打赏
  • 举报
回复
我试试吧
Yansharp 2004-11-22
  • 打赏
  • 举报
回复
struts中自带的上传,原码如下:
package org.apache.struts.webapp.upload;


import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;



/**
* This class takes the UploadForm and retrieves the text value
* and file attributes and puts them in the request for the display.jsp
* page to display them
*
* @author Mike Schachter
* @version $Revision: 1.3 $ $Date: 2004/03/14 06:23:52 $
*/


public class UploadAction extends Action
{
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {

if (form instanceof UploadForm) {

//this line is here for when the input page is upload-utf8.jsp,
//it sets the correct character encoding for the response
String encoding = request.getCharacterEncoding();
if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8")))
{
response.setContentType("text/html; charset=utf-8");
}

UploadForm theForm = (UploadForm) form;

//retrieve the text data
String text = theForm.getTheText();

//retrieve the query string value
String queryValue = theForm.getQueryParam();

//retrieve the file representation
FormFile file = theForm.getTheFile();

//retrieve the file name
String fileName= file.getFileName();

//retrieve the content type
String contentType = file.getContentType();

boolean writeFile = theForm.getWriteFile();

//retrieve the file size
String size = (file.getFileSize() + " bytes");

String data = null;

try {
//retrieve the file data
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream stream = file.getInputStream();
if (!writeFile) {
//only write files out that are less than 1MB
if (file.getFileSize() < (4*1024000)) {

byte[] buffer = new byte[8192];
int bytesRead = 0;
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
baos.write(buffer, 0, bytesRead);
}
data = new String(baos.toByteArray());
}
else {
data = new String("The file is greater than 4MB, " +
" and has not been written to stream." +
" File Size: " + file.getFileSize() + " bytes. This is a" +
" limitation of this particular web application, hard-coded" +
" in org.apache.struts.webapp.upload.UploadAction");
}
}
else {
//write the file to the file specified
OutputStream bos = new FileOutputStream(theForm.getFilePath());
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.close();
data = "The file has been written to \"" + theForm.getFilePath() + "\"";
}
//close the stream
stream.close();
}
catch (FileNotFoundException fnfe) {
return null;
}
catch (IOException ioe) {
return null;
}

//place the data into the request for retrieval from display.jsp
request.setAttribute("text", text);
request.setAttribute("queryValue", queryValue);
request.setAttribute("fileName", fileName);
request.setAttribute("contentType", contentType);
request.setAttribute("size", size);
request.setAttribute("data", data);

//destroy the temporary file created
file.destroy();

//return a forward to display.jsp
return mapping.findForward("display");
}

//this shouldn't happen in this example
return null;
}
}
Figgo 2004-11-22
  • 打赏
  • 举报
回复
你是说你先获得了一个客户端的文件路径,再自动上传那个文件对吗?
好像不可以实现
上海老猫 2004-11-22
  • 打赏
  • 举报
回复
但是我又不能用代码写<input type="file" value="file:///c:/a:gif">
这样的话,jspsmartupload怎么收?
bsh614 2004-11-22
  • 打赏
  • 举报
回复
可以用jspsmartupload组件,不错的
aoplo 2004-11-22
  • 打赏
  • 举报
回复
为什么<input type="file">???那你怎么获得你要上传得文件的路径?
对你的这个问题:但是我又不能用代码写<input type="file" value="file:///c:/a:gif">感到不解!!
难道你要指定死上传file:///c:/a:gif这个文件???那你做这个东西有什么意思?不能让客户选择他要上传得文件!!如果你非要上传一个定死的文件完全不必要考虑这种上传得方式。可以试试servlet中直接读客户端的一个指定目录的指定文件。可你知道客户端在哪吗?除非客户端也是固定的。那么干嘛要用b/s的?用c/s的不是更方便!
uestc6055 2004-11-22
  • 打赏
  • 举报
回复
上传也可以用两个servlet来实现,其中一个用来解析用户的上传请求,另一个用来处理上传
加载更多回复(6)

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧