本文整理匯總了Java中org.apache.commons.fileupload.util.Streams.copy方法的典型用法代碼示例。如果您正苦於以下問題:Java Streams.copy方法的具體用法?Java Streams.copy怎麽用?Java Streams.copy使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.commons.fileupload.util.Streams
的用法示例。
在下文中一共展示了Streams.copy方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: doPost
import org.apache.commons.fileupload.util.Streams; //導入方法依賴的package包/類
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletFileUpload upload = new ServletFileUpload();
try{
FileItemIterator iterator = upload.getItemIterator(request);
if (iterator.hasNext()) {
FileItemStream item = iterator.next();
String name = item.getFieldName();
logger.debug("Uploading file '{}' with item name '{}'", item.getName(), name);
InputStream stream = item.openStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
Streams.copy(stream, out, true);
byte[] data = out.toByteArray();
response.setStatus(HttpServletResponse.SC_OK);
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
response.getWriter().print(new String(data));
response.flushBuffer();
}
else {
logger.error("No file found in post request!");
throw new RuntimeException("No file found in post request!");
}
}
catch(Exception e){
logger.error("Unexpected error in FileUploadServlet.doPost: ", e);
throw new RuntimeException(e);
}
}
示例2: doPost
import org.apache.commons.fileupload.util.Streams; //導入方法依賴的package包/類
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//CHECKSTYLE:ON
ServletFileUpload upload = new ServletFileUpload();
try {
FileItemIterator iter = upload.getItemIterator(request);
if (iter.hasNext()) {
FileItemStream item = iter.next();
String name = item.getFieldName();
LOG.debug("Uploading file '{}' with item name '{}'", item.getName(), name);
InputStream stream = item.openStream();
// Process the input stream
ByteArrayOutputStream out = new ByteArrayOutputStream();
Streams.copy(stream, out, true);
byte[] data = out.toByteArray();
cacheService.uploadedFile(name, data);
} else {
LOG.error("No file found in post request!");
throw new RuntimeException("No file found in post request!");
}
} catch (Exception ex) {
LOG.error("Unexpected error in FileUpload.doPost: ", ex);
throw new RuntimeException(ex);
}
}
示例3: doGet
import org.apache.commons.fileupload.util.Streams; //導入方法依賴的package包/類
/**
* 實現多文件的同時上傳
*/
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
System.out.println("===========");
//設置接收的編碼格式
request.setCharacterEncoding("UTF-8");
Date date = new Date();//獲取當前時間
SimpleDateFormat sdfFileName = new SimpleDateFormat("yyyyMMddHHmmss");
SimpleDateFormat sdfFolder = new SimpleDateFormat("yyMM");
// String newfileName = sdfFileName.format(date);//文件名稱
String fileRealPath = "";//文件存放真實地址
String fileRealResistPath = "";//文件存放真實相對路徑
//名稱 界麵編碼 必須 和request 保存一致..否則亂碼
String name = request.getParameter("name");
String id = request.getParameter("id"); //內容的ID,必須先添加內容,然後才能上傳圖片
// String newfileName = name;
String firstFileName="";
// 獲得容器中上傳文件夾所在的物理路徑
String savePath = this.getServletConfig().getServletContext().getRealPath("/") + "upload\\" + id +"\\";
System.out.println("路徑" + savePath+"; name:"+name);
File file = new File(savePath);
if (!file.isDirectory()) {
file.mkdirs();
}
try {
DiskFileItemFactory fac = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(fac);
upload.setHeaderEncoding("UTF-8");
// upload.setFileItemFactory(factory)
System.out.println("request:="+request);
// 獲取多個上傳文件
List fileList = fileList = upload.parseRequest(request);
System.out.println("fileList:"+fileList);
// 遍曆上傳文件寫入磁盤
Iterator it = fileList.iterator();
while (it.hasNext()) {
Object obit = it.next();
if(obit instanceof DiskFileItem){
System.out.println("xxxxxxxxxxxxx");
DiskFileItem item = (DiskFileItem) obit;
// 如果item是文件上傳表單域
// 獲得文件名及路徑
String fileName = item.getName();
if (fileName != null) {
firstFileName=item.getName().substring(item.getName().lastIndexOf("\\")+1);
String formatName = firstFileName.substring(firstFileName.lastIndexOf("."));//獲取文件後綴名
fileRealPath = savePath + fileName;//+ formatName;//文件存放真實地址
BufferedInputStream in = new BufferedInputStream(item.getInputStream());// 獲得文件輸入流
BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(new File(fileRealPath)));// 獲得文件輸出流
Streams.copy(in, outStream, true);// 開始把文件寫到你指定的上傳文件夾
//上傳成功,則插入數據庫
if (new File(fileRealPath).exists()) {
//虛擬路徑賦值
fileRealResistPath=sdfFolder.format(date)+"/"+fileRealPath.substring(fileRealPath.lastIndexOf("\\")+1);
//保存到數據庫
System.out.println("保存到數據庫:");
System.out.println("name:"+name);
System.out.println("虛擬路徑:"+fileRealResistPath);
}
}
}
}
} catch (org.apache.commons.fileupload.FileUploadException ex) {
ex.printStackTrace();
System.out.println("沒有上傳文件");
return;
}
response.getWriter().write("1");
}
示例4: readBodyData
import org.apache.commons.fileupload.util.Streams; //導入方法依賴的package包/類
/**
* <p>Reads <code>body-data</code> from the current
* <code>encapsulation</code> and writes its contents into the
* output <code>Stream</code>.
* <p>
* <p>Arbitrary large amounts of data can be processed by this
* method using a constant size buffer. (see {@link
* #MultipartStream(InputStream, byte[], int,
* MultipartStreamCopy.ProgressNotifier) constructor}).
*
* @param output The <code>Stream</code> to write data into. May
* be null, in which case this method is equivalent
* to {@link #discardBodyData()}.
* @return the amount of data written.
* @throws MalformedStreamException if the stream ends unexpectedly.
* @throws IOException if an i/o error occurs.
*/
public int readBodyData(OutputStream output)
throws MalformedStreamException, IOException {
final InputStream istream = newInputStream();
return (int) Streams.copy(istream, output, false);
}
示例5: readBodyData
import org.apache.commons.fileupload.util.Streams; //導入方法依賴的package包/類
/**
* <p>Reads <code>body-data</code> from the current
* <code>encapsulation</code> and writes its contents into the
* output <code>Stream</code>.
*
* <p>Arbitrary large amounts of data can be processed by this
* method using a constant size buffer. (see {@link
* #MultipartStream(InputStream,byte[],int,
* MultipartStream.ProgressNotifier) constructor}).
*
* @param output The <code>Stream</code> to write data into. May
* be null, in which case this method is equivalent
* to {@link #discardBodyData()}.
*
* @return the amount of data written.
*
* @throws MalformedStreamException if the stream ends unexpectedly.
* @throws IOException if an i/o error occurs.
*/
public int readBodyData(OutputStream output)
throws MalformedStreamException, IOException {
final InputStream istream = newInputStream();
return (int) Streams.copy(istream, output, false);
}
示例6: readBodyData
import org.apache.commons.fileupload.util.Streams; //導入方法依賴的package包/類
/**
* <p>Reads <code>body-data</code> from the current
* <code>encapsulation</code> and writes its contents into the
* output <code>Stream</code>.
*
* <p>Arbitrary large amounts of data can be processed by this
* method using a constant size buffer. (see {@link
* #MultipartStream(InputStream,byte[],int, ProgressNotifier) constructor}).
*
* @param output The <code>Stream</code> to write data into. May
* be null, in which case this method is equivalent
* to {@link #discardBodyData()}.
*
* @return the amount of data written.
*
* @throws MalformedStreamException if the stream ends unexpectedly.
* @throws IOException if an i/o error occurs.
*/
public int readBodyData(OutputStream output)
throws MalformedStreamException, IOException {
final InputStream istream = newInputStream();
return (int) Streams.copy(istream, output, false);
}